nuklear.h 836 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590
  1. /*
  2. Nuklear - 1.40.0 - public domain
  3. no warrenty implied; use at your own risk.
  4. authored from 2015-2017 by Micha Mettke
  5. ABOUT:
  6. This is a minimal state graphical user interface single header toolkit
  7. written in ANSI C and licensed under public domain.
  8. It was designed as a simple embeddable user interface for application and does
  9. not have any dependencies, a default renderbackend or OS window and input handling
  10. but instead provides a very modular library approach by using simple input state
  11. for input and draw commands describing primitive shapes as output.
  12. So instead of providing a layered library that tries to abstract over a number
  13. of platform and render backends it only focuses on the actual UI.
  14. VALUES:
  15. - Graphical user interface toolkit
  16. - Single header library
  17. - Written in C89 (a.k.a. ANSI C or ISO C90)
  18. - Small codebase (~17kLOC)
  19. - Focus on portability, efficiency and simplicity
  20. - No dependencies (not even the standard library if not wanted)
  21. - Fully skinnable and customizable
  22. - Low memory footprint with total memory control if needed or wanted
  23. - UTF-8 support
  24. - No global or hidden state
  25. - Customizable library modules (you can compile and use only what you need)
  26. - Optional font baker and vertex buffer output
  27. USAGE:
  28. This library is self contained in one single header file and can be used either
  29. in header only mode or in implementation mode. The header only mode is used
  30. by default when included and allows including this header in other headers
  31. and does not contain the actual implementation.
  32. The implementation mode requires to define the preprocessor macro
  33. NK_IMPLEMENTATION in *one* .c/.cpp file before #includeing this file, e.g.:
  34. #define NK_IMPLEMENTATION
  35. #include "nuklear.h"
  36. Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
  37. below in header and implementation mode if you want to use additional functionality
  38. or need more control over the library.
  39. IMPORTANT: Every time you include "nuklear.h" you have to define the same flags.
  40. This is very important not doing it either leads to compiler errors
  41. or even worse stack corruptions.
  42. FEATURES:
  43. - Absolutely no platform dependend code
  44. - Memory management control ranging from/to
  45. - Ease of use by allocating everything from standard library
  46. - Control every byte of memory inside the library
  47. - Font handling control ranging from/to
  48. - Use your own font implementation for everything
  49. - Use this libraries internal font baking and handling API
  50. - Drawing output control ranging from/to
  51. - Simple shapes for more high level APIs which already have drawing capabilities
  52. - Hardware accessible anti-aliased vertex buffer output
  53. - Customizable colors and properties ranging from/to
  54. - Simple changes to color by filling a simple color table
  55. - Complete control with ability to use skinning to decorate widgets
  56. - Bendable UI library with widget ranging from/to
  57. - Basic widgets like buttons, checkboxes, slider, ...
  58. - Advanced widget like abstract comboboxes, contextual menus,...
  59. - Compile time configuration to only compile what you need
  60. - Subset which can be used if you do not want to link or use the standard library
  61. - Can be easily modified to only update on user input instead of frame updates
  62. OPTIONAL DEFINES:
  63. NK_PRIVATE
  64. If defined declares all functions as static, so they can only be accessed
  65. inside the file that contains the implementation
  66. NK_INCLUDE_FIXED_TYPES
  67. If defined it will include header <stdint.h> for fixed sized types
  68. otherwise nuklear tries to select the correct type. If that fails it will
  69. throw a compiler error and you have to select the correct types yourself.
  70. <!> If used needs to be defined for implementation and header <!>
  71. NK_INCLUDE_DEFAULT_ALLOCATOR
  72. if defined it will include header <stdlib.h> and provide additional functions
  73. to use this library without caring for memory allocation control and therefore
  74. ease memory management.
  75. <!> Adds the standard library with malloc and free so don't define if you
  76. don't want to link to the standard library <!>
  77. <!> If used needs to be defined for implementation and header <!>
  78. NK_INCLUDE_STANDARD_IO
  79. if defined it will include header <stdio.h> and provide
  80. additional functions depending on file loading.
  81. <!> Adds the standard library with fopen, fclose,... so don't define this
  82. if you don't want to link to the standard library <!>
  83. <!> If used needs to be defined for implementation and header <!>
  84. NK_INCLUDE_STANDARD_VARARGS
  85. if defined it will include header <stdarg.h> and provide
  86. additional functions depending on variable arguments
  87. <!> Adds the standard library with va_list and so don't define this if
  88. you don't want to link to the standard library<!>
  89. <!> If used needs to be defined for implementation and header <!>
  90. NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  91. Defining this adds a vertex draw command list backend to this
  92. library, which allows you to convert queue commands into vertex draw commands.
  93. This is mainly if you need a hardware accessible format for OpenGL, DirectX,
  94. Vulkan, Metal,...
  95. <!> If used needs to be defined for implementation and header <!>
  96. NK_INCLUDE_FONT_BAKING
  97. Defining this adds `stb_truetype` and `stb_rect_pack` implementation
  98. to this library and provides font baking and rendering.
  99. If you already have font handling or do not want to use this font handler
  100. you don't have to define it.
  101. <!> If used needs to be defined for implementation and header <!>
  102. NK_INCLUDE_DEFAULT_FONT
  103. Defining this adds the default font: ProggyClean.ttf into this library
  104. which can be loaded into a font atlas and allows using this library without
  105. having a truetype font
  106. <!> Enabling this adds ~12kb to global stack memory <!>
  107. <!> If used needs to be defined for implementation and header <!>
  108. NK_INCLUDE_COMMAND_USERDATA
  109. Defining this adds a userdata pointer into each command. Can be useful for
  110. example if you want to provide custom shaders depending on the used widget.
  111. Can be combined with the style structures.
  112. <!> If used needs to be defined for implementation and header <!>
  113. NK_BUTTON_TRIGGER_ON_RELEASE
  114. Different platforms require button clicks occuring either on buttons being
  115. pressed (up to down) or released (down to up).
  116. By default this library will react on buttons being pressed, but if you
  117. define this it will only trigger if a button is released.
  118. <!> If used it is only required to be defined for the implementation part <!>
  119. NK_ZERO_COMMAND_MEMORY
  120. Defining this will zero out memory for each drawing command added to a
  121. drawing queue (inside nk_command_buffer_push). Zeroing command memory
  122. is very useful for fast checking (using memcmp) if command buffers are
  123. equal and avoid drawing frames when nothing on screen has changed since
  124. previous frame.
  125. NK_ASSERT
  126. If you don't define this, nuklear will use <assert.h> with assert().
  127. <!> Adds the standard library so define to nothing of not wanted <!>
  128. <!> If used needs to be defined for implementation and header <!>
  129. NK_BUFFER_DEFAULT_INITIAL_SIZE
  130. Initial buffer size allocated by all buffers while using the default allocator
  131. functions included by defining NK_INCLUDE_DEFAULT_ALLOCATOR. If you don't
  132. want to allocate the default 4k memory then redefine it.
  133. <!> If used needs to be defined for implementation and header <!>
  134. NK_MAX_NUMBER_BUFFER
  135. Maximum buffer size for the conversion buffer between float and string
  136. Under normal circumstances this should be more than sufficient.
  137. <!> If used needs to be defined for implementation and header <!>
  138. NK_INPUT_MAX
  139. Defines the max number of bytes which can be added as text input in one frame.
  140. Under normal circumstances this should be more than sufficient.
  141. <!> If used it is only required to be defined for the implementation part <!>
  142. NK_MEMSET
  143. You can define this to 'memset' or your own memset implementation
  144. replacement. If not nuklear will use its own version.
  145. <!> If used it is only required to be defined for the implementation part <!>
  146. NK_MEMCPY
  147. You can define this to 'memcpy' or your own memcpy implementation
  148. replacement. If not nuklear will use its own version.
  149. <!> If used it is only required to be defined for the implementation part <!>
  150. NK_SQRT
  151. You can define this to 'sqrt' or your own sqrt implementation
  152. replacement. If not nuklear will use its own slow and not highly
  153. accurate version.
  154. <!> If used it is only required to be defined for the implementation part <!>
  155. NK_SIN
  156. You can define this to 'sinf' or your own sine implementation
  157. replacement. If not nuklear will use its own approximation implementation.
  158. <!> If used it is only required to be defined for the implementation part <!>
  159. NK_COS
  160. You can define this to 'cosf' or your own cosine implementation
  161. replacement. If not nuklear will use its own approximation implementation.
  162. <!> If used it is only required to be defined for the implementation part <!>
  163. NK_STRTOD
  164. You can define this to `strtod` or your own string to double conversion
  165. implementation replacement. If not defined nuklear will use its own
  166. imprecise and possibly unsafe version (does not handle nan or infinity!).
  167. <!> If used it is only required to be defined for the implementation part <!>
  168. NK_DTOA
  169. You can define this to `dtoa` or your own double to string conversion
  170. implementation replacement. If not defined nuklear will use its own
  171. imprecise and possibly unsafe version (does not handle nan or infinity!).
  172. <!> If used it is only required to be defined for the implementation part <!>
  173. NK_VSNPRINTF
  174. If you define `NK_INCLUDE_STANDARD_VARARGS` as well as `NK_INCLUDE_STANDARD_IO`
  175. and want to be safe define this to `vsnprintf` on compilers supporting
  176. later versions of C or C++. By default nuklear will check for your stdlib version
  177. in C as well as compiler version in C++. if `vsnprintf` is available
  178. it will define it to `vsnprintf` directly. If not defined and if you have
  179. older versions of C or C++ it will be defined to `vsprintf` which is unsafe.
  180. <!> If used it is only required to be defined for the implementation part <!>
  181. NK_BYTE
  182. NK_INT16
  183. NK_UINT16
  184. NK_INT32
  185. NK_UINT32
  186. NK_SIZE_TYPE
  187. NK_POINTER_TYPE
  188. If you compile without NK_USE_FIXED_TYPE then a number of standard types
  189. will be selected and compile time validated. If they are incorrect you can
  190. define the correct types by overloading these type defines.
  191. CREDITS:
  192. Developed by Micha Mettke and every direct or indirect contributor.
  193. Embeds stb_texedit, stb_truetype and stb_rectpack by Sean Barret (public domain)
  194. Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license).
  195. Big thank you to Omar Cornut (ocornut@github) for his imgui library and
  196. giving me the inspiration for this library, Casey Muratori for handmade hero
  197. and his original immediate mode graphical user interface idea and Sean
  198. Barret for his amazing single header libraries which restored my faith
  199. in libraries and brought me to create some of my own.
  200. LICENSE:
  201. This software is dual-licensed to the public domain and under the following
  202. license: you are granted a perpetual, irrevocable license to copy, modify,
  203. publish and distribute this file as you see fit.
  204. */
  205. #ifndef NK_NUKLEAR_H_
  206. #define NK_NUKLEAR_H_
  207. #ifdef __cplusplus
  208. extern "C" {
  209. #endif
  210. /*
  211. * ==============================================================
  212. *
  213. * CONSTANTS
  214. *
  215. * ===============================================================
  216. */
  217. #define NK_UNDEFINED (-1.0f)
  218. #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
  219. #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
  220. #ifndef NK_INPUT_MAX
  221. #define NK_INPUT_MAX 16
  222. #endif
  223. #ifndef NK_MAX_NUMBER_BUFFER
  224. #define NK_MAX_NUMBER_BUFFER 64
  225. #endif
  226. #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
  227. #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
  228. #endif
  229. /*
  230. * ==============================================================
  231. *
  232. * HELPER
  233. *
  234. * ===============================================================
  235. */
  236. #ifndef NK_API
  237. #ifdef NK_PRIVATE
  238. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
  239. #define NK_API static inline
  240. #elif defined(__cplusplus)
  241. #define NK_API static inline
  242. #else
  243. #define NK_API static
  244. #endif
  245. #else
  246. #define NK_API extern
  247. #endif
  248. #endif
  249. #define NK_INTERN static
  250. #define NK_STORAGE static
  251. #define NK_GLOBAL static
  252. #define NK_FLAG(x) (1 << (x))
  253. #define NK_STRINGIFY(x) #x
  254. #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
  255. #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
  256. #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
  257. #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
  258. #ifdef _MSC_VER
  259. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
  260. #else
  261. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
  262. #endif
  263. #ifndef NK_STATIC_ASSERT
  264. #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
  265. #endif
  266. #ifndef NK_FILE_LINE
  267. #ifdef _MSC_VER
  268. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
  269. #else
  270. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
  271. #endif
  272. #endif
  273. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  274. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  275. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  276. /*
  277. * ===============================================================
  278. *
  279. * BASIC
  280. *
  281. * ===============================================================
  282. */
  283. #ifdef NK_INCLUDE_FIXED_TYPES
  284. #include <stdint.h>
  285. #define NK_INT8 int8_t
  286. #define NK_UINT8 uint8_t
  287. #define NK_INT16 int16_t
  288. #define NK_UINT16 uint16_t
  289. #define NK_INT32 int32_t
  290. #define NK_UINT32 uint32_t
  291. #define NK_SIZE_TYPE uintptr_t
  292. #define NK_POINTER_TYPE uintptr_t
  293. #else
  294. #ifndef NK_INT8
  295. #define NK_INT8 char
  296. #endif
  297. #ifndef NK_UINT8
  298. #define NK_UINT8 unsigned char
  299. #endif
  300. #ifndef NK_INT16
  301. #define NK_INT16 signed short
  302. #endif
  303. #ifndef NK_UINT16
  304. #define NK_UINT16 unsigned short
  305. #endif
  306. #ifndef NK_INT32
  307. #if defined(_MSC_VER)
  308. #define NK_INT32 __int32
  309. #else
  310. #define NK_INT32 signed int
  311. #endif
  312. #endif
  313. #ifndef NK_UINT32
  314. #if defined(_MSC_VER)
  315. #define NK_UINT32 unsigned __int32
  316. #else
  317. #define NK_UINT32 unsigned int
  318. #endif
  319. #endif
  320. #ifndef NK_SIZE_TYPE
  321. #if defined(_WIN64) && defined(_MSC_VER)
  322. #define NK_SIZE_TYPE unsigned __int64
  323. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  324. #define NK_SIZE_TYPE unsigned __int32
  325. #elif defined(__GNUC__) || defined(__clang__)
  326. #if defined(__x86_64__) || defined(__ppc64__)
  327. #define NK_SIZE_TYPE unsigned long
  328. #else
  329. #define NK_SIZE_TYPE unsigned int
  330. #endif
  331. #else
  332. #define NK_SIZE_TYPE unsigned long
  333. #endif
  334. #endif
  335. #ifndef NK_POINTER_TYPE
  336. #if defined(_WIN64) && defined(_MSC_VER)
  337. #define NK_POINTER_TYPE unsigned __int64
  338. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  339. #define NK_POINTER_TYPE unsigned __int32
  340. #elif defined(__GNUC__) || defined(__clang__)
  341. #if defined(__x86_64__) || defined(__ppc64__)
  342. #define NK_POINTER_TYPE unsigned long
  343. #else
  344. #define NK_POINTER_TYPE unsigned int
  345. #endif
  346. #else
  347. #define NK_POINTER_TYPE unsigned long
  348. #endif
  349. #endif
  350. #endif
  351. typedef NK_INT8 nk_char;
  352. typedef NK_UINT8 nk_uchar;
  353. typedef NK_UINT8 nk_byte;
  354. typedef NK_INT16 nk_short;
  355. typedef NK_UINT16 nk_ushort;
  356. typedef NK_INT32 nk_int;
  357. typedef NK_UINT32 nk_uint;
  358. typedef NK_SIZE_TYPE nk_size;
  359. typedef NK_POINTER_TYPE nk_ptr;
  360. typedef nk_uint nk_hash;
  361. typedef nk_uint nk_flags;
  362. typedef nk_uint nk_rune;
  363. /* Make sure correct type size:
  364. * This will fire with a negative subscript error if the type sizes
  365. * are set incorrectly by the compiler, and compile out if not */
  366. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  367. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  368. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  369. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  370. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  371. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  372. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  373. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  374. NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
  375. /* ============================================================================
  376. *
  377. * API
  378. *
  379. * =========================================================================== */
  380. struct nk_buffer;
  381. struct nk_allocator;
  382. struct nk_command_buffer;
  383. struct nk_draw_command;
  384. struct nk_convert_config;
  385. struct nk_style_item;
  386. struct nk_text_edit;
  387. struct nk_draw_list;
  388. struct nk_user_font;
  389. struct nk_panel;
  390. struct nk_context;
  391. struct nk_draw_vertex_layout_element;
  392. struct nk_style_button;
  393. struct nk_style_toggle;
  394. struct nk_style_selectable;
  395. struct nk_style_slide;
  396. struct nk_style_progress;
  397. struct nk_style_scrollbar;
  398. struct nk_style_edit;
  399. struct nk_style_property;
  400. struct nk_style_chart;
  401. struct nk_style_combo;
  402. struct nk_style_tab;
  403. struct nk_style_window_header;
  404. struct nk_style_window;
  405. enum {nk_false, nk_true};
  406. struct nk_color {nk_byte r,g,b,a;};
  407. struct nk_colorf {float r,g,b,a;};
  408. struct nk_vec2 {float x,y;};
  409. struct nk_vec2i {short x, y;};
  410. struct nk_rect {float x,y,w,h;};
  411. struct nk_recti {short x,y,w,h;};
  412. typedef char nk_glyph[NK_UTF_SIZE];
  413. typedef union {void *ptr; int id;} nk_handle;
  414. struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
  415. struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
  416. struct nk_scroll {nk_uint x, y;};
  417. enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
  418. enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
  419. enum nk_modify {NK_FIXED = nk_false, NK_MODIFIABLE = nk_true};
  420. enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
  421. enum nk_collapse_states {NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true};
  422. enum nk_show_states {NK_HIDDEN = nk_false, NK_SHOWN = nk_true};
  423. enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
  424. enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
  425. enum nk_color_format {NK_RGB, NK_RGBA};
  426. enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
  427. enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
  428. enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
  429. typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
  430. typedef void (*nk_plugin_free)(nk_handle, void *old);
  431. typedef int(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
  432. typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
  433. typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
  434. struct nk_allocator {
  435. nk_handle userdata;
  436. nk_plugin_alloc alloc;
  437. nk_plugin_free free;
  438. };
  439. enum nk_symbol_type {
  440. NK_SYMBOL_NONE,
  441. NK_SYMBOL_X,
  442. NK_SYMBOL_UNDERSCORE,
  443. NK_SYMBOL_CIRCLE_SOLID,
  444. NK_SYMBOL_CIRCLE_OUTLINE,
  445. NK_SYMBOL_RECT_SOLID,
  446. NK_SYMBOL_RECT_OUTLINE,
  447. NK_SYMBOL_TRIANGLE_UP,
  448. NK_SYMBOL_TRIANGLE_DOWN,
  449. NK_SYMBOL_TRIANGLE_LEFT,
  450. NK_SYMBOL_TRIANGLE_RIGHT,
  451. NK_SYMBOL_PLUS,
  452. NK_SYMBOL_MINUS,
  453. NK_SYMBOL_MAX
  454. };
  455. /* =============================================================================
  456. *
  457. * CONTEXT
  458. *
  459. * =============================================================================*/
  460. /* Contexts are the main entry point and the majestro of nuklear and contain all required state.
  461. * They are used for window, memory, input, style, stack, commands and time management and need
  462. * to be passed into all nuklear GUI specific functions.
  463. *
  464. * Usage
  465. * -------------------
  466. * To use a context it first has to be initialized which can be achieved by calling
  467. * one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
  468. * Each takes in a font handle and a specific way of handling memory. Memory control
  469. * hereby ranges from standard library to just specifing a fixed sized block of memory
  470. * which nuklear has to manage itself from.
  471. *
  472. * struct nk_context ctx;
  473. * nk_init_xxx(&ctx, ...);
  474. * while (1) {
  475. * [...]
  476. * nk_clear(&ctx);
  477. * }
  478. * nk_free(&ctx);
  479. *
  480. * Reference
  481. * -------------------
  482. * nk_init_default - Initializes context with standard library memory alloction (malloc,free)
  483. * nk_init_fixed - Initializes context from single fixed size memory block
  484. * nk_init - Initializes context with memory allocator callbacks for alloc and free
  485. * nk_init_custom - Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
  486. * nk_clear - Called at the end of the frame to reset and prepare the context for the next frame
  487. * nk_free - Shutdown and free all memory allocated inside the context
  488. * nk_set_user_data - Utility function to pass user data to draw command
  489. */
  490. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  491. /* nk_init_default - Initializes a `nk_context` struct with a default standard library allocator.
  492. * Should be used if you don't want to be bothered with memory management in nuklear.
  493. * Parameters:
  494. * @ctx must point to an either stack or heap allocated `nk_context` struct
  495. * @font must point to a previously initialized font handle for more info look at font documentation
  496. * Return values:
  497. * true(1) on success
  498. * false(0) on failure */
  499. NK_API int nk_init_default(struct nk_context*, const struct nk_user_font*);
  500. #endif
  501. /* nk_init_fixed - Initializes a `nk_context` struct from a single fixed size memory block
  502. * Should be used if you want complete control over nuklears memory management.
  503. * Especially recommended for system with little memory or systems with virtual memory.
  504. * For the later case you can just allocate for example 16MB of virtual memory
  505. * and only the required amount of memory will actually be commited.
  506. * IMPORTANT: make sure the passed memory block is aligned correctly for `nk_draw_commands`
  507. * Parameters:
  508. * @ctx must point to an either stack or heap allocated `nk_context` struct
  509. * @memory must point to a previously allocated memory block
  510. * @size must contain the total size of @memory
  511. * @font must point to a previously initialized font handle for more info look at font documentation
  512. * Return values:
  513. * true(1) on success
  514. * false(0) on failure */
  515. NK_API int nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
  516. /* nk_init - Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
  517. * memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
  518. * interface to nuklear. Can be useful for cases like monitoring memory consumption.
  519. * Parameters:
  520. * @ctx must point to an either stack or heap allocated `nk_context` struct
  521. * @alloc must point to a previously allocated memory allocator
  522. * @font must point to a previously initialized font handle for more info look at font documentation
  523. * Return values:
  524. * true(1) on success
  525. * false(0) on failure */
  526. NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
  527. /* nk_init_custom - Initializes a `nk_context` struct from two different either fixed or growing
  528. * buffers. The first buffer is for allocating draw commands while the second buffer is
  529. * used for allocating windows, panels and state tables.
  530. * Parameters:
  531. * @ctx must point to an either stack or heap allocated `nk_context` struct
  532. * @cmds must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
  533. * @pool must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
  534. * @font must point to a previously initialized font handle for more info look at font documentation
  535. * Return values:
  536. * true(1) on success
  537. * false(0) on failure */
  538. NK_API int nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
  539. /* nk_clear - Resets the context state at the end of the frame. This includes mostly
  540. * garbage collector tasks like removing windows or table not called and therefore
  541. * used anymore.
  542. * Parameters:
  543. * @ctx must point to a previously initialized `nk_context` struct */
  544. NK_API void nk_clear(struct nk_context*);
  545. /* nk_free - Frees all memory allocated by nuklear. Not needed if context was
  546. * initialized with `nk_init_fixed`.
  547. * Parameters:
  548. * @ctx must point to a previously initialized `nk_context` struct */
  549. NK_API void nk_free(struct nk_context*);
  550. #ifdef NK_INCLUDE_COMMAND_USERDATA
  551. /* nk_set_user_data - Sets the currently passed userdata passed down into each draw command.
  552. * Parameters:
  553. * @ctx must point to a previously initialized `nk_context` struct
  554. * @data handle with either pointer or index to be passed into every draw commands */
  555. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  556. #endif
  557. /* =============================================================================
  558. *
  559. * INPUT
  560. *
  561. * =============================================================================*/
  562. /* The input API is responsible for holding the current input state composed of
  563. * mouse, key and text input states.
  564. * It is worth noting that no direct os or window handling is done in nuklear.
  565. * Instead all input state has to be provided by platform specific code. This in one hand
  566. * expects more work from the user and complicates usage but on the other hand
  567. * provides simple abstraction over a big number of platforms, libraries and other
  568. * already provided functionality.
  569. *
  570. * Usage
  571. * -------------------
  572. * Input state needs to be provided to nuklear by first calling `nk_input_begin`
  573. * which resets internal state like delta mouse position and button transistions.
  574. * After `nk_input_begin` all current input state needs to be provided. This includes
  575. * mouse motion, button and key pressed and released, text input and scrolling.
  576. * Both event- or state-based input handling are supported by this API
  577. * and should work without problems. Finally after all input state has been
  578. * mirrored `nk_input_end` needs to be called to finish input process.
  579. *
  580. * struct nk_context ctx;
  581. * nk_init_xxx(&ctx, ...);
  582. * while (1) {
  583. * Event evt;
  584. * nk_input_begin(&ctx);
  585. * while (GetEvent(&evt)) {
  586. * if (evt.type == MOUSE_MOVE)
  587. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  588. * else if (evt.type == ...) {
  589. * ...
  590. * }
  591. * }
  592. * nk_input_end(&ctx);
  593. * [...]
  594. * nk_clear(&ctx);
  595. * }
  596. * nk_free(&ctx);
  597. *
  598. * Reference
  599. * -------------------
  600. * nk_input_begin - Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
  601. * nk_input_motion - Mirrors mouse cursor position
  602. * nk_input_key - Mirrors key state with either pressed or released
  603. * nk_input_button - Mirrors mouse button state with either pressed or released
  604. * nk_input_scroll - Mirrors mouse scroll values
  605. * nk_input_char - Adds a single ASCII text character into an internal text buffer
  606. * nk_input_glyph - Adds a single multi-byte UTF-8 character into an internal text buffer
  607. * nk_input_unicode - Adds a single unicode rune into an internal text buffer
  608. * 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
  609. */
  610. enum nk_keys {
  611. NK_KEY_NONE,
  612. NK_KEY_SHIFT,
  613. NK_KEY_CTRL,
  614. NK_KEY_DEL,
  615. NK_KEY_ENTER,
  616. NK_KEY_TAB,
  617. NK_KEY_BACKSPACE,
  618. NK_KEY_COPY,
  619. NK_KEY_CUT,
  620. NK_KEY_PASTE,
  621. NK_KEY_UP,
  622. NK_KEY_DOWN,
  623. NK_KEY_LEFT,
  624. NK_KEY_RIGHT,
  625. /* Shortcuts: text field */
  626. NK_KEY_TEXT_INSERT_MODE,
  627. NK_KEY_TEXT_REPLACE_MODE,
  628. NK_KEY_TEXT_RESET_MODE,
  629. NK_KEY_TEXT_LINE_START,
  630. NK_KEY_TEXT_LINE_END,
  631. NK_KEY_TEXT_START,
  632. NK_KEY_TEXT_END,
  633. NK_KEY_TEXT_UNDO,
  634. NK_KEY_TEXT_REDO,
  635. NK_KEY_TEXT_SELECT_ALL,
  636. NK_KEY_TEXT_WORD_LEFT,
  637. NK_KEY_TEXT_WORD_RIGHT,
  638. /* Shortcuts: scrollbar */
  639. NK_KEY_SCROLL_START,
  640. NK_KEY_SCROLL_END,
  641. NK_KEY_SCROLL_DOWN,
  642. NK_KEY_SCROLL_UP,
  643. NK_KEY_MAX
  644. };
  645. enum nk_buttons {
  646. NK_BUTTON_LEFT,
  647. NK_BUTTON_MIDDLE,
  648. NK_BUTTON_RIGHT,
  649. NK_BUTTON_DOUBLE,
  650. NK_BUTTON_MAX
  651. };
  652. /* nk_input_begin - Begins the input mirroring process by resetting text, scroll
  653. * mouse previous mouse position and movement as well as key state transistions,
  654. * Parameters:
  655. * @ctx must point to an previously initialized `nk_context` struct */
  656. NK_API void nk_input_begin(struct nk_context*);
  657. /* nk_input_motion - Mirros current mouse position to nuklear
  658. * Parameters:
  659. * @ctx must point to an previously initialized `nk_context` struct
  660. * @x must constain an integer describing the current mouse cursor x-position
  661. * @y must constain an integer describing the current mouse cursor y-position */
  662. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  663. /* nk_input_key - Mirros state of a specific key to nuklear
  664. * Parameters:
  665. * @ctx must point to an previously initialized `nk_context` struct
  666. * @key must be any value specified in enum `nk_keys` that needs to be mirrored
  667. * @down must be 0 for key is up and 1 for key is down */
  668. NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
  669. /* nk_input_button - Mirros the state of a specific mouse button to nuklear
  670. * Parameters:
  671. * @ctx must point to an previously initialized `nk_context` struct
  672. * @nk_buttons must be any value specified in enum `nk_buttons` that needs to be mirrored
  673. * @x must constain an integer describing mouse cursor x-position on click up/down
  674. * @y must constain an integer describing mouse cursor y-position on click up/down
  675. * @down must be 0 for key is up and 1 for key is down */
  676. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
  677. /* nk_input_char - Copies a single ASCII character into an internal text buffer
  678. * This is basically a helper function to quickly push ASCII characters into
  679. * nuklear. Note that you can only push up to NK_INPUT_MAX bytes into
  680. * struct `nk_input` between `nk_input_begin` and `nk_input_end`.
  681. * Parameters:
  682. * @ctx must point to an previously initialized `nk_context` struct
  683. * @c must be a single ASCII character preferable one that can be printed */
  684. NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
  685. /* nk_input_char - Copies a single ASCII character into an internal text buffer
  686. * This is basically a helper function to quickly push ASCII characters into
  687. * nuklear. Note that you can only push up to NK_INPUT_MAX bytes into
  688. * struct `nk_input` between `nk_input_begin` and `nk_input_end`.
  689. * Parameters:
  690. * @ctx must point to an previously initialized `nk_context` struct
  691. * @c must be a single ASCII character preferable one that can be printed */
  692. NK_API void nk_input_char(struct nk_context*, char);
  693. /* nk_input_unicode - Converts a encoded unicode rune into UTF-8 and copies the result
  694. * into an internal text buffer.
  695. * Note that you can only push up to NK_INPUT_MAX bytes into
  696. * struct `nk_input` between `nk_input_begin` and `nk_input_end`.
  697. * Parameters:
  698. * @ctx must point to an previously initialized `nk_context` struct
  699. * @glyph UTF-32 uncode codepoint */
  700. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  701. /* nk_input_unicode - Converts a unicode rune into UTF-8 and copies the result
  702. * into an internal text buffer.
  703. * Note that you can only push up to NK_INPUT_MAX bytes into
  704. * struct `nk_input` between `nk_input_begin` and `nk_input_end`.
  705. * Parameters:
  706. * @ctx must point to an previously initialized `nk_context` struct
  707. * @glyph UTF-32 uncode codepoint */
  708. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  709. /* nk_input_end - End the input mirroring process by resetting mouse grabbing
  710. * state to ensure the mouse cursor is not grabbed indefinitely.
  711. * Parameters:
  712. * @ctx must point to an previously initialized `nk_context` struct */
  713. NK_API void nk_input_end(struct nk_context*);
  714. /* =============================================================================
  715. *
  716. * DRAWING
  717. *
  718. * =============================================================================*/
  719. /* This library was designed to be render backend agnostic so it does
  720. * not draw anything to screen directly. Instead all drawn shapes, widgets
  721. * are made of, are buffered into memory and make up a command queue.
  722. * Each frame therefore fills the command buffer with draw commands
  723. * that then need to be executed by the user and his own render backend.
  724. * After that the command buffer needs to be cleared and a new frame can be
  725. * started. It is probably important to note that the command buffer is the main
  726. * drawing API and the optional vertex buffer API only takes this format and
  727. * converts it into a hardware accessible format.
  728. *
  729. * Usage
  730. * -------------------
  731. * To draw all draw commands accumulated over a frame you need your own render
  732. * backend able to draw a number of 2D primitives. This includes at least
  733. * filled and stroked rectangles, circles, text, lines, triangles and scissors.
  734. * As soon as this criterion is met you can iterate over each draw command
  735. * and execute each draw command in a interpreter like fashion:
  736. *
  737. * const struct nk_command *cmd = 0;
  738. * nk_foreach(cmd, &ctx) {
  739. * switch (cmd->type) {
  740. * case NK_COMMAND_LINE:
  741. * your_draw_line_function(...)
  742. * break;
  743. * case NK_COMMAND_RECT
  744. * your_draw_rect_function(...)
  745. * break;
  746. * case ...:
  747. * [...]
  748. * }
  749. *
  750. * In program flow context draw commands need to be executed after input has been
  751. * gathered and the complete UI with windows and their contained widgets have
  752. * been executed and before calling `nk_clear` which frees all previously
  753. * allocated draw commands.
  754. *
  755. * struct nk_context ctx;
  756. * nk_init_xxx(&ctx, ...);
  757. * while (1) {
  758. * Event evt;
  759. * nk_input_begin(&ctx);
  760. * while (GetEvent(&evt)) {
  761. * if (evt.type == MOUSE_MOVE)
  762. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  763. * else if (evt.type == [...]) {
  764. * [...]
  765. * }
  766. * }
  767. * nk_input_end(&ctx);
  768. *
  769. * [...]
  770. *
  771. * const struct nk_command *cmd = 0;
  772. * nk_foreach(cmd, &ctx) {
  773. * switch (cmd->type) {
  774. * case NK_COMMAND_LINE:
  775. * your_draw_line_function(...)
  776. * break;
  777. * case NK_COMMAND_RECT
  778. * your_draw_rect_function(...)
  779. * break;
  780. * case ...:
  781. * [...]
  782. * }
  783. * nk_clear(&ctx);
  784. * }
  785. * nk_free(&ctx);
  786. *
  787. * You probably noticed that you have to draw all of the UI each frame which is
  788. * quite wasteful. While the actual UI updating loop is quite fast rendering
  789. * without actually needing it is not. So there are multiple things you could do.
  790. *
  791. * First is only update on input. This of course is only an option if your
  792. * application only depends on the UI and does not require any outside calculations.
  793. * If you actually only update on input make sure to update the UI two times each
  794. * frame and call `nk_clear` directly after the first pass and only draw in
  795. * the second pass. In addition it is recommended to also add additional timers
  796. * to make sure the UI is not drawn more than a fixed number of frames per second.
  797. *
  798. * struct nk_context ctx;
  799. * nk_init_xxx(&ctx, ...);
  800. * while (1) {
  801. * [...wait for input ]
  802. *
  803. * [...do two UI passes ...]
  804. * do_ui(...)
  805. * nk_clear(&ctx);
  806. * do_ui(...)
  807. *
  808. * const struct nk_command *cmd = 0;
  809. * nk_foreach(cmd, &ctx) {
  810. * switch (cmd->type) {
  811. * case NK_COMMAND_LINE:
  812. * your_draw_line_function(...)
  813. * break;
  814. * case NK_COMMAND_RECT
  815. * your_draw_rect_function(...)
  816. * break;
  817. * case ...:
  818. * [...]
  819. * }
  820. * nk_clear(&ctx);
  821. * }
  822. * nk_free(&ctx);
  823. *
  824. * The second probably more applicable trick is to only draw if anything changed.
  825. * It is not really useful for applications with continous draw loop but
  826. * quite useful for desktop applications. To actually get nuklear to only
  827. * draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
  828. * allocate a memory buffer that will store each unique drawing output.
  829. * After each frame you compare the draw command memory inside the library
  830. * with your allocated buffer by memcmp. If memcmp detects differences
  831. * you have to copy the command buffer into the allocated buffer
  832. * and then draw like usual (this example uses fixed memory but you could
  833. * use dynamically allocated memory).
  834. *
  835. * [... other defines ...]
  836. * #define NK_ZERO_COMMAND_MEMORY
  837. * #include "nuklear.h"
  838. *
  839. * struct nk_context ctx;
  840. * void *last = calloc(1,64*1024);
  841. * void *buf = calloc(1,64*1024);
  842. * nk_init_fixed(&ctx, buf, 64*1024);
  843. * while (1) {
  844. * [...input...]
  845. * [...ui...]
  846. *
  847. * void *cmds = nk_buffer_memory(&ctx.memory);
  848. * if (memcmp(cmds, last, ctx.memory.allocated)) {
  849. * memcpy(last,cmds,ctx.memory.allocated);
  850. * const struct nk_command *cmd = 0;
  851. * nk_foreach(cmd, &ctx) {
  852. * switch (cmd->type) {
  853. * case NK_COMMAND_LINE:
  854. * your_draw_line_function(...)
  855. * break;
  856. * case NK_COMMAND_RECT
  857. * your_draw_rect_function(...)
  858. * break;
  859. * case ...:
  860. * [...]
  861. * }
  862. * }
  863. * }
  864. * nk_clear(&ctx);
  865. * }
  866. * nk_free(&ctx);
  867. *
  868. * Finally while using draw commands makes sense for higher abstracted platforms like
  869. * X11 and Win32 or drawing libraries it is often desirable to use graphics
  870. * hardware directly. Therefore it is possible to just define
  871. * `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
  872. * To access the vertex output you first have to convert all draw commands into
  873. * vertexes by calling `nk_convert` which takes in your prefered vertex format.
  874. * After successfully converting all draw commands just iterate over and execute all
  875. * vertex draw commands:
  876. *
  877. * struct nk_convert_config cfg = {};
  878. * static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  879. * {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
  880. * {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
  881. * {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
  882. * {NK_VERTEX_LAYOUT_END}
  883. * };
  884. * cfg.shape_AA = NK_ANTI_ALIASING_ON;
  885. * cfg.line_AA = NK_ANTI_ALIASING_ON;
  886. * cfg.vertex_layout = vertex_layout;
  887. * cfg.vertex_size = sizeof(struct your_vertex);
  888. * cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
  889. * cfg.circle_segment_count = 22;
  890. * cfg.curve_segment_count = 22;
  891. * cfg.arc_segment_count = 22;
  892. * cfg.global_alpha = 1.0f;
  893. * cfg.null = dev->null;
  894. *
  895. * struct nk_buffer cmds, verts, idx;
  896. * nk_buffer_init_default(&cmds);
  897. * nk_buffer_init_default(&verts);
  898. * nk_buffer_init_default(&idx);
  899. * nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
  900. * nk_draw_foreach(cmd, &ctx, &cmds) {
  901. * if (!cmd->elem_count) continue;
  902. * [...]
  903. * }
  904. * nk_buffer_free(&cms);
  905. * nk_buffer_free(&verts);
  906. * nk_buffer_free(&idx);
  907. *
  908. * Reference
  909. * -------------------
  910. * nk__begin - Returns the first draw command in the context draw command list to be drawn
  911. * nk__next - Increments the draw command iterator to the next command inside the context draw command list
  912. * nk_foreach - Iteratates over each draw command inside the context draw command list
  913. *
  914. * nk_convert - Converts from the abstract draw commands list into a hardware accessable vertex format
  915. * nk__draw_begin - Returns the first vertex command in the context vertex draw list to be executed
  916. * nk__draw_next - Increments the vertex command iterator to the next command inside the context vertex command list
  917. * nk__draw_end - Returns the end of the vertex draw list
  918. * nk_draw_foreach - Iterates over each vertex draw command inside the vertex draw list
  919. */
  920. enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
  921. enum nk_convert_result {
  922. NK_CONVERT_SUCCESS = 0,
  923. NK_CONVERT_INVALID_PARAM = 1,
  924. NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
  925. NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
  926. NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
  927. };
  928. struct nk_draw_null_texture {
  929. nk_handle texture; /* texture handle to a texture with a white pixel */
  930. struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
  931. };
  932. struct nk_convert_config {
  933. float global_alpha; /* global alpha value */
  934. enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
  935. enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
  936. unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
  937. unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
  938. unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
  939. struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
  940. const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
  941. nk_size vertex_size; /* sizeof one vertex for vertex packing */
  942. nk_size vertex_alignment; /* vertex alignment: Can be optained by NK_ALIGNOF */
  943. };
  944. /* nk__begin - Returns a draw command list iterator to iterate all draw
  945. * commands accumulated over one frame.
  946. * Parameters:
  947. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  948. * Return values:
  949. * draw command pointer pointing to the first command inside the draw command list */
  950. NK_API const struct nk_command* nk__begin(struct nk_context*);
  951. /* nk__next - Returns a draw command list iterator to iterate all draw
  952. * Parameters:
  953. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  954. * @cmd must point to an previously a draw command either returned by `nk__begin` or `nk__next`
  955. * Return values:
  956. * draw command pointer pointing to the next command inside the draw command list */
  957. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  958. /* nk_foreach - Iterates over each draw command inside the context draw command list
  959. * Parameters:
  960. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  961. * @cmd pointer initialized to NULL */
  962. #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
  963. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  964. /* nk_convert - converts all internal draw command into vertex draw commands and fills
  965. * three buffers with vertexes, vertex draw commands and vertex indicies. The vertex format
  966. * as well as some other configuration values have to be configurated by filling out a
  967. * `nk_convert_config` struct.
  968. * Parameters:
  969. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  970. * @cmds must point to a previously initialized buffer to hold converted vertex draw commands
  971. * @vertices must point to a previously initialized buffer to hold all produced verticies
  972. * @elements must point to a previously initialized buffer to hold all procudes vertex indicies
  973. * @config must point to a filled out `nk_config` struct to configure the conversion process
  974. * Returns:
  975. * returns NK_CONVERT_SUCCESS on success and a enum nk_convert_result error values if not */
  976. 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*);
  977. /* nk__draw_begin - Returns a draw vertex command buffer iterator to iterate each the vertex draw command buffer
  978. * Parameters:
  979. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  980. * @buf must point to an previously by `nk_convert` filled out vertex draw command buffer
  981. * Return values:
  982. * vertex draw command pointer pointing to the first command inside the vertex draw command buffer */
  983. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  984. /* nk__draw_end - Returns the vertex draw command at the end of the vertex draw command buffer
  985. * Parameters:
  986. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  987. * @buf must point to an previously by `nk_convert` filled out vertex draw command buffer
  988. * Return values:
  989. * vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer */
  990. NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
  991. /* nk__draw_next - Increments the the vertex draw command buffer iterator
  992. * Parameters:
  993. * @cmd must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
  994. * @buf must point to an previously by `nk_convert` filled out vertex draw command buffer
  995. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame
  996. * Return values:
  997. * vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer */
  998. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  999. /* nk_draw_foreach - Iterates over each vertex draw command inside a vertex draw command buffer
  1000. * Parameters:
  1001. * @cmd nk_draw_command pointer set to NULL
  1002. * @buf must point to an previously by `nk_convert` filled out vertex draw command buffer
  1003. * @ctx must point to an previously initialized `nk_context` struct at the end of a frame */
  1004. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  1005. #endif
  1006. /* =============================================================================
  1007. *
  1008. * WINDOW
  1009. *
  1010. * =============================================================================
  1011. * Windows are the main persistent state used inside nuklear and are life time
  1012. * controlled by simply "retouching" (i.e. calling) each window each frame.
  1013. * All widgets inside nuklear can only be added inside function pair `nk_begin_xxx`
  1014. * and `nk_end`. Calling any widgets outside these two functions will result in an
  1015. * assert in debug or no state change in release mode.
  1016. *
  1017. * Each window holds frame persistent state like position, size, flags, state tables,
  1018. * and some garbage collected internal persistent widget state. Each window
  1019. * is linked into a window stack list which determines the drawing and overlapping
  1020. * order. The topmost window thereby is the currently active window.
  1021. *
  1022. * To change window position inside the stack occurs either automatically by
  1023. * user input by being clicked on or programatically by calling `nk_window_focus`.
  1024. * Windows by default are visible unless explicitly being defined with flag
  1025. * `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
  1026. * `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
  1027. * `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.
  1028. *
  1029. * Usage
  1030. * -------------------
  1031. * To create and keep a window you have to call one of the two `nk_begin_xxx`
  1032. * functions to start window declarations and `nk_end` at the end. Furthermore it
  1033. * is recommended to check the return value of `nk_begin_xxx` and only process
  1034. * widgets inside the window if the value is not 0. Either way you have to call
  1035. * `nk_end` at the end of window declarations. Furthmore do not attempt to
  1036. * nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
  1037. * in a segmation fault.
  1038. *
  1039. * if (nk_begin_xxx(...) {
  1040. * [... widgets ...]
  1041. * }
  1042. * nk_end(ctx);
  1043. *
  1044. * In the grand concept window and widget declarations need to occur after input
  1045. * handling and before drawing to screen. Not doing so can result in higher
  1046. * latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
  1047. * is called at the end of the frame. While nuklears default platform backends
  1048. * already call `nk_clear` for you if you write your own backend not calling
  1049. * `nk_clear` can cause asserts or even worse undefined behavior.
  1050. *
  1051. * struct nk_context ctx;
  1052. * nk_init_xxx(&ctx, ...);
  1053. * while (1) {
  1054. * Event evt;
  1055. * nk_input_begin(&ctx);
  1056. * while (GetEvent(&evt)) {
  1057. * if (evt.type == MOUSE_MOVE)
  1058. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  1059. * else if (evt.type == [...]) {
  1060. * nk_input_xxx(...);
  1061. * }
  1062. * }
  1063. * nk_input_end(&ctx);
  1064. *
  1065. * if (nk_begin_xxx(...) {
  1066. * [...]
  1067. * }
  1068. * nk_end(ctx);
  1069. *
  1070. * const struct nk_command *cmd = 0;
  1071. * nk_foreach(cmd, &ctx) {
  1072. * case NK_COMMAND_LINE:
  1073. * your_draw_line_function(...)
  1074. * break;
  1075. * case NK_COMMAND_RECT
  1076. * your_draw_rect_function(...)
  1077. * break;
  1078. * case ...:
  1079. * [...]
  1080. * }
  1081. * nk_clear(&ctx);
  1082. * }
  1083. * nk_free(&ctx);
  1084. *
  1085. * Reference
  1086. * -------------------
  1087. * nk_begin - starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1088. * nk_begin_titled - extended window start with seperated title and identifier to allow multiple windows with same name but not title
  1089. * nk_end - needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
  1090. *
  1091. * nk_window_find - finds and returns the window with give name
  1092. * nk_window_get_bounds - returns a rectangle with screen position and size of the currently processed window.
  1093. * nk_window_get_position - returns the position of the currently processed window
  1094. * nk_window_get_size - returns the size with width and height of the currently processed window
  1095. * nk_window_get_width - returns the width of the currently processed window
  1096. * nk_window_get_height - returns the height of the currently processed window
  1097. * nk_window_get_panel - returns the underlying panel which contains all processing state of the currnet window
  1098. * nk_window_get_content_region - returns the position and size of the currently visible and non-clipped space inside the currently processed window
  1099. * nk_window_get_content_region_min - returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1100. * nk_window_get_content_region_max - returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1101. * nk_window_get_content_region_size - returns the size of the currently visible and non-clipped space inside the currently processed window
  1102. * nk_window_get_canvas - returns the draw command buffer. Can be used to draw custom widgets
  1103. *
  1104. * nk_window_has_focus - returns if the currently processed window is currently active
  1105. * nk_window_is_collapsed - returns if the window with given name is currently minimized/collapsed
  1106. * nk_window_is_closed - returns if the currently processed window was closed
  1107. * nk_window_is_hidden - returns if the currently processed window was hidden
  1108. * nk_window_is_active - same as nk_window_has_focus for some reason
  1109. * nk_window_is_hovered - returns if the currently processed window is currently being hovered by mouse
  1110. * nk_window_is_any_hovered - return if any wndow currently hovered
  1111. * nk_item_is_any_active - returns if any window or widgets is currently hovered or active
  1112. *
  1113. * nk_window_set_bounds - updates position and size of the currently processed window
  1114. * nk_window_set_position - updates position of the currently process window
  1115. * nk_window_set_size - updates the size of the currently processed window
  1116. * nk_window_set_focus - set the currently processed window as active window
  1117. *
  1118. * nk_window_close - closes the window with given window name which deletes the window at the end of the frame
  1119. * nk_window_collapse - collapses the window with given window name
  1120. * nk_window_collapse_if - collapses the window with given window name if the given condition was met
  1121. * nk_window_show - hides a visible or reshows a hidden window
  1122. * nk_window_show_if - hides/shows a window depending on condition
  1123. */
  1124. enum nk_panel_flags {
  1125. NK_WINDOW_BORDER = NK_FLAG(0), /* Draws a border around the window to visually separate window from the background */
  1126. NK_WINDOW_MOVABLE = NK_FLAG(1), /* The movable flag indicates that a window can be moved by user input or by dragging the window header */
  1127. NK_WINDOW_SCALABLE = NK_FLAG(2), /* The scalable flag indicates that a window can be scaled by user input by dragging a scaler icon at the button of the window */
  1128. NK_WINDOW_CLOSABLE = NK_FLAG(3), /* adds a closable icon into the header */
  1129. NK_WINDOW_MINIMIZABLE = NK_FLAG(4), /* adds a minimize icon into the header */
  1130. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5), /* Removes the scrollbar from the window */
  1131. NK_WINDOW_TITLE = NK_FLAG(6), /* Forces a header at the top at the window showing the title */
  1132. NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7), /* Automatically hides the window scrollbar if no user interaction: also requires delta time in `nk_context` to be set each frame */
  1133. NK_WINDOW_BACKGROUND = NK_FLAG(8), /* Always keep window in the background */
  1134. NK_WINDOW_SCALE_LEFT = NK_FLAG(9), /* Puts window scaler in the left-ottom corner instead right-bottom*/
  1135. NK_WINDOW_NO_INPUT = NK_FLAG(10) /* Prevents window of scaling, moving or getting focus */
  1136. };
  1137. /* nk_begin - starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1138. * Parameters:
  1139. * @ctx must point to an previously initialized `nk_context` struct
  1140. * @title window title and identifier. Needs to be persitent over frames to identify the window
  1141. * @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
  1142. * @flags window flags defined in `enum nk_panel_flags` with a number of different window behaviors
  1143. * Return values:
  1144. * returns 1 if the window can be filled up with widgets from this point until `nk_end or 0 otherwise for example if minimized `*/
  1145. NK_API int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1146. /* nk_begin_titled - extended window start with seperated title and identifier to allow multiple windows with same name but not title
  1147. * Parameters:
  1148. * @ctx must point to an previously initialized `nk_context` struct
  1149. * @name window identifier. Needs to be persitent over frames to identify the window
  1150. * @title window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
  1151. * @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
  1152. * @flags window flags defined in `enum nk_panel_flags` with a number of different window behaviors
  1153. * Return values:
  1154. * returns 1 if the window can be filled up with widgets from this point until `nk_end or 0 otherwise `*/
  1155. NK_API int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1156. /* nk_end - needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
  1157. * All widget calls after this functions will result in asserts or no state changes
  1158. * Parameters:
  1159. * @ctx must point to an previously initialized `nk_context` struct */
  1160. NK_API void nk_end(struct nk_context *ctx);
  1161. /* nk_window_find - finds and returns the window with give name
  1162. * Parameters:
  1163. * @ctx must point to an previously initialized `nk_context` struct
  1164. * @name window identifier
  1165. * Return values:
  1166. * returns a `nk_window` struct pointing to the idified window or 0 if no window with given name was found */
  1167. NK_API struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1168. /* nk_window_get_bounds - returns a rectangle with screen position and size of the currently processed window.
  1169. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1170. * Parameters:
  1171. * @ctx must point to an previously initialized `nk_context` struct
  1172. * Return values:
  1173. * returns a `nk_rect` struct with window upper left position and size */
  1174. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1175. /* nk_window_get_position - returns the position of the currently processed window.
  1176. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1177. * Parameters:
  1178. * @ctx must point to an previously initialized `nk_context` struct
  1179. * Return values:
  1180. * returns a `nk_vec2` struct with window upper left position */
  1181. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1182. /* nk_window_get_size - returns the size with width and height of the currently processed window.
  1183. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1184. * Parameters:
  1185. * @ctx must point to an previously initialized `nk_context` struct
  1186. * Return values:
  1187. * returns a `nk_vec2` struct with window size */
  1188. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
  1189. /* nk_window_get_width - returns the width of the currently processed window.
  1190. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1191. * Parameters:
  1192. * @ctx must point to an previously initialized `nk_context` struct
  1193. * Return values:
  1194. * returns the window width */
  1195. NK_API float nk_window_get_width(const struct nk_context*);
  1196. /* nk_window_get_height - returns the height of the currently processed window.
  1197. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1198. * Parameters:
  1199. * @ctx must point to an previously initialized `nk_context` struct
  1200. * Return values:
  1201. * returns the window height */
  1202. NK_API float nk_window_get_height(const struct nk_context*);
  1203. /* nk_window_get_panel - returns the underlying panel which contains all processing state of the currnet window.
  1204. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1205. * Parameters:
  1206. * @ctx must point to an previously initialized `nk_context` struct
  1207. * Return values:
  1208. * returns a pointer to window internal `nk_panel` state. DO NOT keep this pointer around it is only valid until `nk_end` */
  1209. NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
  1210. /* nk_window_get_content_region - returns the position and size of the currently visible and non-clipped space inside the currently processed window.
  1211. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1212. * Parameters:
  1213. * @ctx must point to an previously initialized `nk_context` struct
  1214. * Return values:
  1215. * returns `nk_rect` struct with screen position and size (no scrollbar offset) of the visible space inside the current window */
  1216. NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
  1217. /* nk_window_get_content_region_min - returns the upper left position of the currently visible and non-clipped space inside the currently processed window.
  1218. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1219. * Parameters:
  1220. * @ctx must point to an previously initialized `nk_context` struct
  1221. * Return values:
  1222. * returns `nk_vec2` struct with upper left screen position (no scrollbar offset) of the visible space inside the current window */
  1223. NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
  1224. /* nk_window_get_content_region_max - returns the lower right screen position of the currently visible and non-clipped space inside the currently processed window.
  1225. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1226. * Parameters:
  1227. * @ctx must point to an previously initialized `nk_context` struct
  1228. * Return values:
  1229. * returns `nk_vec2` struct with lower right screen position (no scrollbar offset) of the visible space inside the current window */
  1230. NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
  1231. /* nk_window_get_content_region_size - returns the size of the currently visible and non-clipped space inside the currently processed window
  1232. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1233. * Parameters:
  1234. * @ctx must point to an previously initialized `nk_context` struct
  1235. * Return values:
  1236. * returns `nk_vec2` struct with size the visible space inside the current window */
  1237. NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
  1238. /* nk_window_get_canvas - returns the draw command buffer. Can be used to draw custom widgets
  1239. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1240. * Parameters:
  1241. * @ctx must point to an previously initialized `nk_context` struct
  1242. * Return values:
  1243. * returns a pointer to window internal `nk_command_buffer` struct used as drawing canvas. Can be used to do custom drawing */
  1244. NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
  1245. /* nk_window_has_focus - returns if the currently processed window is currently active
  1246. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1247. * Parameters:
  1248. * @ctx must point to an previously initialized `nk_context` struct
  1249. * Return values:
  1250. * returns 0 if current window is not active or 1 if it is */
  1251. NK_API int nk_window_has_focus(const struct nk_context*);
  1252. /* nk_window_is_collapsed - returns if the window with given name is currently minimized/collapsed
  1253. * Parameters:
  1254. * @ctx must point to an previously initialized `nk_context` struct
  1255. * @name of window you want to check is collapsed
  1256. * Return values:
  1257. * returns 1 if current window is minimized and 0 if window not found or is not minimized */
  1258. NK_API int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1259. /* nk_window_is_closed - returns if the window with given name was closed by calling `nk_close`
  1260. * Parameters:
  1261. * @ctx must point to an previously initialized `nk_context` struct
  1262. * @name of window you want to check is closed
  1263. * Return values:
  1264. * returns 1 if current window was closed or 0 window not found or not closed */
  1265. NK_API int nk_window_is_closed(struct nk_context*, const char*);
  1266. /* nk_window_is_hidden - returns if the window with given name is hidden
  1267. * Parameters:
  1268. * @ctx must point to an previously initialized `nk_context` struct
  1269. * @name of window you want to check is hidden
  1270. * Return values:
  1271. * returns 1 if current window is hidden or 0 window not found or visible */
  1272. NK_API int nk_window_is_hidden(struct nk_context*, const char*);
  1273. /* nk_window_is_active - same as nk_window_has_focus for some reason
  1274. * Parameters:
  1275. * @ctx must point to an previously initialized `nk_context` struct
  1276. * @name of window you want to check is hidden
  1277. * Return values:
  1278. * returns 1 if current window is active or 0 window not found or not active */
  1279. NK_API int nk_window_is_active(struct nk_context*, const char*);
  1280. /* nk_window_is_hovered - return if the current window is being hovered
  1281. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1282. * Parameters:
  1283. * @ctx must point to an previously initialized `nk_context` struct
  1284. * Return values:
  1285. * returns 1 if current window is hovered or 0 otherwise */
  1286. NK_API int nk_window_is_hovered(struct nk_context*);
  1287. /* nk_window_is_any_hovered - returns if the any window is being hovered
  1288. * Parameters:
  1289. * @ctx must point to an previously initialized `nk_context` struct
  1290. * Return values:
  1291. * returns 1 if any window is hovered or 0 otherwise */
  1292. NK_API int nk_window_is_any_hovered(struct nk_context*);
  1293. /* nk_item_is_any_active - returns if the any window is being hovered or any widget is currently active.
  1294. * Can be used to decide if input should be processed by UI or your specific input handling.
  1295. * Example could be UI and 3D camera to move inside a 3D space.
  1296. * Parameters:
  1297. * @ctx must point to an previously initialized `nk_context` struct
  1298. * Return values:
  1299. * returns 1 if any window is hovered or any item is active or 0 otherwise */
  1300. NK_API int nk_item_is_any_active(struct nk_context*);
  1301. /* nk_window_set_bounds - updates position and size of the currently processed window
  1302. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1303. * Parameters:
  1304. * @ctx must point to an previously initialized `nk_context` struct
  1305. * @bounds points to a `nk_rect` struct with the new position and size of currently active window */
  1306. NK_API void nk_window_set_bounds(struct nk_context*, struct nk_rect bounds);
  1307. /* nk_window_set_position - updates position of the currently processed window
  1308. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1309. * Parameters:
  1310. * @ctx must point to an previously initialized `nk_context` struct
  1311. * @pos points to a `nk_vec2` struct with the new position of currently active window */
  1312. NK_API void nk_window_set_position(struct nk_context*, struct nk_vec2 pos);
  1313. /* nk_window_set_size - updates size of the currently processed window
  1314. * IMPORTANT: only call this function between calls `nk_begin_xxx` and `nk_end`
  1315. * Parameters:
  1316. * @ctx must point to an previously initialized `nk_context` struct
  1317. * @bounds points to a `nk_vec2` struct with the new size of currently active window */
  1318. NK_API void nk_window_set_size(struct nk_context*, struct nk_vec2);
  1319. /* nk_window_set_focus - sets the window with given name as active
  1320. * Parameters:
  1321. * @ctx must point to an previously initialized `nk_context` struct
  1322. * @name of the window to be set active */
  1323. NK_API void nk_window_set_focus(struct nk_context*, const char *name);
  1324. /* nk_window_close - closed a window and marks it for being freed at the end of the frame
  1325. * Parameters:
  1326. * @ctx must point to an previously initialized `nk_context` struct
  1327. * @name of the window to be closed */
  1328. NK_API void nk_window_close(struct nk_context *ctx, const char *name);
  1329. /* nk_window_collapse - updates collapse state of a window with given name
  1330. * Parameters:
  1331. * @ctx must point to an previously initialized `nk_context` struct
  1332. * @name of the window to be either collapse or maximize */
  1333. NK_API void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1334. /* nk_window_collapse - updates collapse state of a window with given name if given condition is met
  1335. * Parameters:
  1336. * @ctx must point to an previously initialized `nk_context` struct
  1337. * @name of the window to be either collapse or maximize
  1338. * @state the window should be put into
  1339. * @condition that has to be true to actually commit the collsage state change */
  1340. NK_API void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1341. /* nk_window_show - updates visibility state of a window with given name
  1342. * Parameters:
  1343. * @ctx must point to an previously initialized `nk_context` struct
  1344. * @name of the window to be either collapse or maximize
  1345. * @state with either visible or hidden to modify the window with */
  1346. NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1347. /* nk_window_show_if - updates visibility state of a window with given name if a given condition is met
  1348. * Parameters:
  1349. * @ctx must point to an previously initialized `nk_context` struct
  1350. * @name of the window to be either collapse or maximize
  1351. * @state with either visible or hidden to modify the window with
  1352. * @condition that has to be true to actually commit the visible state change */
  1353. NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1354. /* =============================================================================
  1355. *
  1356. * LAYOUT
  1357. *
  1358. * ============================================================================= */
  1359. /* Layouting in general describes placing widget inside a window with position and size.
  1360. * While in this particular implementation there are five different APIs for layouting
  1361. * each with different trade offs between control and ease of use.
  1362. *
  1363. * All layouting methodes in this library are based around the concept of a row.
  1364. * A row has a height the window content grows by and a number of columns and each
  1365. * layouting method specifies how each widget is placed inside the row.
  1366. * After a row has been allocated by calling a layouting functions and then
  1367. * filled with widgets will advance an internal pointer over the allocated row.
  1368. *
  1369. * To acually define a layout you just call the appropriate layouting function
  1370. * and each subsequnetial widget call will place the widget as specified. Important
  1371. * here is that if you define more widgets then columns defined inside the layout
  1372. * functions it will allocate the next row without you having to make another layouting
  1373. * call.
  1374. *
  1375. * Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
  1376. * is that you have to define the row height for each. However the row height
  1377. * often depends on the height of the font.
  1378. *
  1379. * To fix that internally nuklear uses a minimum row height that is set to the
  1380. * height plus padding of currently active font and overwrites the row height
  1381. * value if zero.
  1382. *
  1383. * If you manually want to change the minimum row height then
  1384. * use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
  1385. * reset it back to be derived from font height.
  1386. *
  1387. * Also if you change the font in nuklear it will automatically change the minimum
  1388. * row height for you and. This means if you change the font but still want
  1389. * a minimum row height smaller than the font you have to repush your value.
  1390. *
  1391. * For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
  1392. * layouting method in combination with a cassowary constraint solver (there are
  1393. * some versions on github with permissive license model) to take over all control over widget
  1394. * layouting yourself. However for quick and dirty layouting using all the other layouting
  1395. * functions should be fine.
  1396. *
  1397. * Usage
  1398. * -------------------
  1399. * 1.) nk_layout_row_dynamic
  1400. * The easiest layouting function is `nk_layout_row_dynamic`. It provides each
  1401. * widgets with same horizontal space inside the row and dynamically grows
  1402. * if the owning window grows in width. So the number of columns dictates
  1403. * the size of each widget dynamically by formula:
  1404. *
  1405. * widget_width = (window_width - padding - spacing) * (1/colum_count)
  1406. *
  1407. * Just like all other layouting APIs if you define more widget than columns this
  1408. * library will allocate a new row and keep all layouting parameters previously
  1409. * defined.
  1410. *
  1411. * if (nk_begin_xxx(...) {
  1412. * // first row with height: 30 composed of two widgets
  1413. * nk_layout_row_dynamic(&ctx, 30, 2);
  1414. * nk_widget(...);
  1415. * nk_widget(...);
  1416. *
  1417. * // second row with same parameter as defined above
  1418. * nk_widget(...);
  1419. * nk_widget(...);
  1420. *
  1421. * // third row uses 0 for height which will use auto layouting
  1422. * nk_layout_row_dynamic(&ctx, 0, 2);
  1423. * nk_widget(...);
  1424. * nk_widget(...);
  1425. * }
  1426. * nk_end(...);
  1427. *
  1428. * 2.) nk_layout_row_static
  1429. * Another easy layouting function is `nk_layout_row_static`. It provides each
  1430. * widget with same horizontal pixel width inside the row and does not grow
  1431. * if the owning window scales smaller or bigger.
  1432. *
  1433. * if (nk_begin_xxx(...) {
  1434. * // first row with height: 30 composed of two widgets with width: 80
  1435. * nk_layout_row_static(&ctx, 30, 80, 2);
  1436. * nk_widget(...);
  1437. * nk_widget(...);
  1438. *
  1439. * // second row with same parameter as defined above
  1440. * nk_widget(...);
  1441. * nk_widget(...);
  1442. *
  1443. * // third row uses 0 for height which will use auto layouting
  1444. * nk_layout_row_static(&ctx, 0, 80, 2);
  1445. * nk_widget(...);
  1446. * nk_widget(...);
  1447. * }
  1448. * nk_end(...);
  1449. *
  1450. * 3.) nk_layout_row_xxx
  1451. * A little bit more advanced layouting API are functions `nk_layout_row_begin`,
  1452. * `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
  1453. * specify each column pixel or window ratio in a row. It supports either
  1454. * directly setting per column pixel width or widget window ratio but not
  1455. * both. Furthermore it is a immediate mode API so each value is directly
  1456. * pushed before calling a widget. Therefore the layout is not automatically
  1457. * repeating like the last two layouting functions.
  1458. *
  1459. * if (nk_begin_xxx(...) {
  1460. * // first row with height: 25 composed of two widgets with width 60 and 40
  1461. * nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  1462. * nk_layout_row_push(ctx, 60);
  1463. * nk_widget(...);
  1464. * nk_layout_row_push(ctx, 40);
  1465. * nk_widget(...);
  1466. * nk_layout_row_end(ctx);
  1467. *
  1468. * // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
  1469. * nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
  1470. * nk_layout_row_push(ctx, 0.25f);
  1471. * nk_widget(...);
  1472. * nk_layout_row_push(ctx, 0.75f);
  1473. * nk_widget(...);
  1474. * nk_layout_row_end(ctx);
  1475. *
  1476. * // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
  1477. * nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
  1478. * nk_layout_row_push(ctx, 0.25f);
  1479. * nk_widget(...);
  1480. * nk_layout_row_push(ctx, 0.75f);
  1481. * nk_widget(...);
  1482. * nk_layout_row_end(ctx);
  1483. * }
  1484. * nk_end(...);
  1485. *
  1486. * 4.) nk_layout_row
  1487. * The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
  1488. * functions. Instead of pushing either pixel or window ratio for every widget
  1489. * it allows to define it by array. The trade of for less control is that
  1490. * `nk_layout_row` is automatically repeating. Otherwise the behavior is the
  1491. * same.
  1492. *
  1493. * if (nk_begin_xxx(...) {
  1494. * // two rows with height: 30 composed of two widgets with width 60 and 40
  1495. * const float size[] = {60,40};
  1496. * nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  1497. * nk_widget(...);
  1498. * nk_widget(...);
  1499. * nk_widget(...);
  1500. * nk_widget(...);
  1501. *
  1502. * // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
  1503. * const float ratio[] = {0.25, 0.75};
  1504. * nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  1505. * nk_widget(...);
  1506. * nk_widget(...);
  1507. * nk_widget(...);
  1508. * nk_widget(...);
  1509. *
  1510. * // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
  1511. * const float ratio[] = {0.25, 0.75};
  1512. * nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  1513. * nk_widget(...);
  1514. * nk_widget(...);
  1515. * nk_widget(...);
  1516. * nk_widget(...);
  1517. * }
  1518. * nk_end(...);
  1519. *
  1520. * 5.) nk_layout_row_template_xxx
  1521. * The most complex and second most flexible API is a simplified flexbox version without
  1522. * line wrapping and weights for dynamic widgets. It is an immediate mode API but
  1523. * unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
  1524. * before calling the templated widgets.
  1525. * The row template layout has three different per widget size specifier. The first
  1526. * one is the static widget size specifier with fixed widget pixel width. They do
  1527. * not grow if the row grows and will always stay the same. The second size
  1528. * specifier is nk_layout_row_template_push_variable which defines a
  1529. * minumum widget size but it also can grow if more space is available not taken
  1530. * by other widgets. Finally there are dynamic widgets which are completly flexible
  1531. * and unlike variable widgets can even shrink to zero if not enough space
  1532. * is provided.
  1533. *
  1534. * if (nk_begin_xxx(...) {
  1535. * // two rows with height: 30 composed of three widgets
  1536. * nk_layout_row_template_begin(ctx, 30);
  1537. * nk_layout_row_template_push_dynamic(ctx);
  1538. * nk_layout_row_template_push_variable(ctx, 80);
  1539. * nk_layout_row_template_push_static(ctx, 80);
  1540. * nk_layout_row_template_end(ctx);
  1541. *
  1542. * nk_widget(...); // dynamic widget can go to zero if not enough space
  1543. * nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
  1544. * nk_widget(...); // static widget with fixed 80 pixel width
  1545. *
  1546. * // second row same layout
  1547. * nk_widget(...);
  1548. * nk_widget(...);
  1549. * nk_widget(...);
  1550. * }
  1551. * nk_end(...);
  1552. *
  1553. * 6.) nk_layout_space_xxx
  1554. * Finally the most flexible API directly allows you to place widgets inside the
  1555. * window. The space layout API is an immediate mode API which does not support
  1556. * row auto repeat and directly sets position and size of a widget. Position
  1557. * and size hereby can be either specified as ratio of alloated space or
  1558. * allocated space local position and pixel size. Since this API is quite
  1559. * powerfull there are a number of utility functions to get the available space
  1560. * and convert between local allocated space and screen space.
  1561. *
  1562. * if (nk_begin_xxx(...) {
  1563. * // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  1564. * nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
  1565. * nk_layout_space_push(ctx, nk_rect(0,0,150,200));
  1566. * nk_widget(...);
  1567. * nk_layout_space_push(ctx, nk_rect(200,200,100,200));
  1568. * nk_widget(...);
  1569. * nk_layout_space_end(ctx);
  1570. *
  1571. * // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  1572. * nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
  1573. * nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
  1574. * nk_widget(...);
  1575. * nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
  1576. * nk_widget(...);
  1577. * }
  1578. * nk_end(...);
  1579. *
  1580. * Reference
  1581. * -------------------
  1582. * nk_layout_set_min_row_height - set the currently used minimum row height to a specified value
  1583. * nk_layout_reset_min_row_height - resets the currently used minimum row height to font height
  1584. *
  1585. * nk_layout_widget_bounds - calculates current width a static layout row can fit inside a window
  1586. * nk_layout_ratio_from_pixel - utility functions to calculate window ratio from pixel size
  1587. *
  1588. * nk_layout_row_dynamic - current layout is divided into n same sized gowing columns
  1589. * nk_layout_row_static - current layout is divided into n same fixed sized columns
  1590. * nk_layout_row_begin - starts a new row with given height and number of columns
  1591. * nk_layout_row_push - pushes another column with given size or window ratio
  1592. * nk_layout_row_end - finished previously started row
  1593. * nk_layout_row - specifies row columns in array as either window ratio or size
  1594. *
  1595. * nk_layout_row_template_begin - begins the row template declaration
  1596. * nk_layout_row_template_push_dynamic - adds a dynamic column that dynamically grows and can go to zero if not enough space
  1597. * nk_layout_row_template_push_variable - adds a variable column that dynamically grows but does not shrink below specified pixel width
  1598. * nk_layout_row_template_push_static - adds a static column that does not grow and will always have the same size
  1599. * nk_layout_row_template_end - marks the end of the row template
  1600. *
  1601. * nk_layout_space_begin - begins a new layouting space that allows to specify each widgets position and size
  1602. * nk_layout_space_push - pushes position and size of the next widget in own coordiante space either as pixel or ratio
  1603. * nk_layout_space_end - marks the end of the layouting space
  1604. *
  1605. * nk_layout_space_bounds - callable after nk_layout_space_begin and returns total space allocated
  1606. * nk_layout_space_to_screen - convertes vector from nk_layout_space coordiant space into screen space
  1607. * nk_layout_space_to_local - convertes vector from screem space into nk_layout_space coordinates
  1608. * nk_layout_space_rect_to_screen - convertes rectangle from nk_layout_space coordiant space into screen space
  1609. * nk_layout_space_rect_to_local - convertes rectangle from screem space into nk_layout_space coordinates
  1610. */
  1611. /* nk_layout_set_min_row_height - sets the currently used minimum row height.
  1612. * IMPORTANT: The passed height needs to include both your prefered row height
  1613. * as well as padding. No internal padding is added.
  1614. * Parameters:
  1615. * @ctx must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  1616. * @height new minimum row height to be used for auto generating the row height */
  1617. NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
  1618. /* nk_layout_reset_min_row_height - Reset the currently used minimum row height
  1619. * back to font height + text padding + additional padding (style_window.min_row_height_padding)
  1620. * Parameters:
  1621. * @ctx must point to an previously initialized `nk_context` struct after call `nk_begin_xxx` */
  1622. NK_API void nk_layout_reset_min_row_height(struct nk_context*);
  1623. /* nk_layout_widget_bounds - returns the width of the next row allocate by one of the layouting functions
  1624. * Parameters:
  1625. * @ctx must point to an previously initialized `nk_context` */
  1626. NK_API struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  1627. /* nk_layout_ratio_from_pixel - utility functions to calculate window ratio from pixel size
  1628. * Parameters:
  1629. * @ctx must point to an previously initialized `nk_context`
  1630. * @pixel_width to convert to window ratio */
  1631. NK_API float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  1632. /* nk_layout_row_dynamic - Sets current row layout to share horizontal space
  1633. * between @cols number of widgets evenly. Once called all subsequent widget
  1634. * calls greater than @cols will allocate a new row with same layout.
  1635. * Parameters:
  1636. * @ctx must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  1637. * @row_height holds height of each widget in row or zero for auto layouting
  1638. * @cols number of widget inside row */
  1639. NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  1640. /* nk_layout_row_static - Sets current row layout to fill @cols number of widgets
  1641. * in row with same @item_width horizontal size. Once called all subsequent widget
  1642. * calls greater than @cols will allocate a new row with same layout.
  1643. * Parameters:
  1644. * @ctx must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  1645. * @height holds row height to allocate from panel for widget height
  1646. * @item_width holds width of each widget in row
  1647. * @cols number of widget inside row */
  1648. NK_API void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  1649. /* nk_layout_row_begin - Starts a new dynamic or fixed row with given height and columns.
  1650. * Parameters:
  1651. * @ctx must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  1652. * @fmt either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  1653. * @row_height holds height of each widget in row or zero for auto layouting
  1654. * @cols number of widget inside row */
  1655. NK_API void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  1656. /* nk_layout_row_push - Specifies either window ratio or width of a single column
  1657. * Parameters:
  1658. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_row_begin`
  1659. * @value either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call */
  1660. NK_API void nk_layout_row_push(struct nk_context*, float value);
  1661. /* nk_layout_row_end - finished previously started row
  1662. * Parameters:
  1663. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_row_begin` */
  1664. NK_API void nk_layout_row_end(struct nk_context*);
  1665. /* nk_layout_row - specifies row columns in array as either window ratio or size
  1666. * Parameters:
  1667. * @ctx must point to an previously initialized `nk_context`
  1668. * @fmt either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  1669. * @row_height holds height of each widget in row or zero for auto layouting
  1670. * @cols number of widget inside row */
  1671. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  1672. /* nk_layout_row_template_begin - Begins the row template declaration
  1673. * Parameters:
  1674. * @ctx must point to an previously initialized `nk_context` struct
  1675. * @row_height holds height of each widget in row or zero for auto layouting */
  1676. NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
  1677. /* nk_layout_row_template_push_dynamic - adds a dynamic column that dynamically grows and can go to zero if not enough space
  1678. * Parameters:
  1679. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_row_template_begin` */
  1680. NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
  1681. /* nk_layout_row_template_push_variable - adds a variable column that dynamically grows but does not shrink below specified pixel width
  1682. * Parameters:
  1683. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_row_template_begin`
  1684. * @min_width holds the minimum pixel width the next column must be */
  1685. NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  1686. /* nk_layout_row_template_push_static - adds a static column that does not grow and will always have the same size
  1687. * Parameters:
  1688. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_row_template_begin`
  1689. * @width holds the absolulte pixel width value the next column must be */
  1690. NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
  1691. /* nk_layout_row_template_end - marks the end of the row template
  1692. * Parameters:
  1693. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_row_template_begin` */
  1694. NK_API void nk_layout_row_template_end(struct nk_context*);
  1695. /* nk_layout_space_begin - begins a new layouting space that allows to specify each widgets position and size.
  1696. * Parameters:
  1697. * @ctx must point to an previously initialized `nk_context` struct
  1698. * @fmt either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  1699. * @row_height holds height of each widget in row or zero for auto layouting
  1700. * @widget_count number of widgets inside row */
  1701. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  1702. /* nk_layout_space_push - pushes position and size of the next widget in own coordiante space either as pixel or ratio
  1703. * Parameters:
  1704. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  1705. * @bounds position and size in laoyut space local coordinates */
  1706. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect);
  1707. /* nk_layout_space_end - marks the end of the layout space
  1708. * Parameters:
  1709. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin` */
  1710. NK_API void nk_layout_space_end(struct nk_context*);
  1711. /* nk_layout_space_bounds - returns total space allocated for `nk_layout_space`
  1712. * Parameters:
  1713. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin` */
  1714. NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
  1715. /* nk_layout_space_to_screen - convertes vector from nk_layout_space coordiant space into screen space
  1716. * Parameters:
  1717. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  1718. * @vec position to convert from layout space into screen coordinate space */
  1719. NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  1720. /* nk_layout_space_to_screen - convertes vector from layout space into screen space
  1721. * Parameters:
  1722. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  1723. * @vec position to convert from screen space into layout coordinate space */
  1724. NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  1725. /* nk_layout_space_rect_to_screen - convertes rectangle from screen space into layout space
  1726. * Parameters:
  1727. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  1728. * @bounds rectangle to convert from layout space into screen space */
  1729. NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  1730. /* nk_layout_space_rect_to_local - convertes rectangle from layout space into screen space
  1731. * Parameters:
  1732. * @ctx must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  1733. * @bounds rectangle to convert from screen space into layout space */
  1734. NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  1735. /* =============================================================================
  1736. *
  1737. * GROUP
  1738. *
  1739. * ============================================================================= */
  1740. NK_API int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  1741. NK_API int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char*, nk_flags);
  1742. NK_API int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll*, const char *title, nk_flags);
  1743. NK_API void nk_group_scrolled_end(struct nk_context*);
  1744. NK_API void nk_group_end(struct nk_context*);
  1745. /* =============================================================================
  1746. *
  1747. * LIST VIEW
  1748. *
  1749. * ============================================================================= */
  1750. struct nk_list_view {
  1751. /* public: */
  1752. int begin, end, count;
  1753. /* private: */
  1754. int total_height;
  1755. struct nk_context *ctx;
  1756. nk_uint *scroll_pointer;
  1757. nk_uint scroll_value;
  1758. };
  1759. 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);
  1760. NK_API void nk_list_view_end(struct nk_list_view*);
  1761. /* =============================================================================
  1762. *
  1763. * TREE
  1764. *
  1765. * ============================================================================= */
  1766. #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__)
  1767. #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)
  1768. 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);
  1769. #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__)
  1770. #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)
  1771. 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);
  1772. NK_API void nk_tree_pop(struct nk_context*);
  1773. NK_API int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  1774. 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);
  1775. NK_API void nk_tree_state_pop(struct nk_context*);
  1776. /* =============================================================================
  1777. *
  1778. * WIDGET
  1779. *
  1780. * ============================================================================= */
  1781. enum nk_widget_layout_states {
  1782. NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
  1783. NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
  1784. NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
  1785. };
  1786. enum nk_widget_states {
  1787. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  1788. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
  1789. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
  1790. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
  1791. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
  1792. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
  1793. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
  1794. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
  1795. };
  1796. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  1797. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
  1798. NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
  1799. NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
  1800. NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
  1801. NK_API float nk_widget_width(struct nk_context*);
  1802. NK_API float nk_widget_height(struct nk_context*);
  1803. NK_API int nk_widget_is_hovered(struct nk_context*);
  1804. NK_API int nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
  1805. NK_API int nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, int down);
  1806. NK_API void nk_spacing(struct nk_context*, int cols);
  1807. /* =============================================================================
  1808. *
  1809. * TEXT
  1810. *
  1811. * ============================================================================= */
  1812. enum nk_text_align {
  1813. NK_TEXT_ALIGN_LEFT = 0x01,
  1814. NK_TEXT_ALIGN_CENTERED = 0x02,
  1815. NK_TEXT_ALIGN_RIGHT = 0x04,
  1816. NK_TEXT_ALIGN_TOP = 0x08,
  1817. NK_TEXT_ALIGN_MIDDLE = 0x10,
  1818. NK_TEXT_ALIGN_BOTTOM = 0x20
  1819. };
  1820. enum nk_text_alignment {
  1821. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
  1822. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
  1823. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
  1824. };
  1825. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  1826. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  1827. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  1828. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  1829. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  1830. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  1831. NK_API void nk_label_wrap(struct nk_context*, const char*);
  1832. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  1833. NK_API void nk_image(struct nk_context*, struct nk_image);
  1834. #ifdef NK_INCLUDE_STANDARD_VARARGS
  1835. NK_API void nk_labelf(struct nk_context*, nk_flags, const char*, ...);
  1836. NK_API void nk_labelf_colored(struct nk_context*, nk_flags align, struct nk_color, const char*,...);
  1837. NK_API void nk_labelf_wrap(struct nk_context*, const char*,...);
  1838. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, const char*,...);
  1839. NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
  1840. NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
  1841. NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
  1842. NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
  1843. NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
  1844. NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
  1845. NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
  1846. #endif
  1847. /* =============================================================================
  1848. *
  1849. * BUTTON
  1850. *
  1851. * ============================================================================= */
  1852. NK_API int nk_button_text(struct nk_context*, const char *title, int len);
  1853. NK_API int nk_button_label(struct nk_context*, const char *title);
  1854. NK_API int nk_button_color(struct nk_context*, struct nk_color);
  1855. NK_API int nk_button_symbol(struct nk_context*, enum nk_symbol_type);
  1856. NK_API int nk_button_image(struct nk_context*, struct nk_image img);
  1857. NK_API int nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
  1858. NK_API int nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  1859. NK_API int nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
  1860. NK_API int nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
  1861. NK_API int nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char *title, int len);
  1862. NK_API int nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char *title);
  1863. NK_API int nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
  1864. NK_API int nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
  1865. 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);
  1866. 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);
  1867. 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);
  1868. 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);
  1869. NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
  1870. NK_API int nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
  1871. NK_API int nk_button_pop_behavior(struct nk_context*);
  1872. /* =============================================================================
  1873. *
  1874. * CHECKBOX
  1875. *
  1876. * ============================================================================= */
  1877. NK_API int nk_check_label(struct nk_context*, const char*, int active);
  1878. NK_API int nk_check_text(struct nk_context*, const char*, int,int active);
  1879. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  1880. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  1881. NK_API int nk_checkbox_label(struct nk_context*, const char*, int *active);
  1882. NK_API int nk_checkbox_text(struct nk_context*, const char*, int, int *active);
  1883. NK_API int nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
  1884. NK_API int nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
  1885. /* =============================================================================
  1886. *
  1887. * RADIO BUTTON
  1888. *
  1889. * ============================================================================= */
  1890. NK_API int nk_radio_label(struct nk_context*, const char*, int *active);
  1891. NK_API int nk_radio_text(struct nk_context*, const char*, int, int *active);
  1892. NK_API int nk_option_label(struct nk_context*, const char*, int active);
  1893. NK_API int nk_option_text(struct nk_context*, const char*, int, int active);
  1894. /* =============================================================================
  1895. *
  1896. * SELECTABLE
  1897. *
  1898. * ============================================================================= */
  1899. NK_API int nk_selectable_label(struct nk_context*, const char*, nk_flags align, int *value);
  1900. NK_API int nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, int *value);
  1901. NK_API int nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, int *value);
  1902. NK_API int nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, int *value);
  1903. NK_API int nk_select_label(struct nk_context*, const char*, nk_flags align, int value);
  1904. NK_API int nk_select_text(struct nk_context*, const char*, int, nk_flags align, int value);
  1905. NK_API int nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, int value);
  1906. NK_API int nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, int value);
  1907. /* =============================================================================
  1908. *
  1909. * SLIDER
  1910. *
  1911. * ============================================================================= */
  1912. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  1913. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  1914. NK_API int nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
  1915. NK_API int nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
  1916. /* =============================================================================
  1917. *
  1918. * PROGRESSBAR
  1919. *
  1920. * ============================================================================= */
  1921. NK_API int nk_progress(struct nk_context*, nk_size *cur, nk_size max, int modifyable);
  1922. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, int modifyable);
  1923. /* =============================================================================
  1924. *
  1925. * COLOR PICKER
  1926. *
  1927. * ============================================================================= */
  1928. NK_API struct nk_color nk_color_picker(struct nk_context*, struct nk_color, enum nk_color_format);
  1929. NK_API int nk_color_pick(struct nk_context*, struct nk_color*, enum nk_color_format);
  1930. /* =============================================================================
  1931. *
  1932. * PROPERTIES
  1933. *
  1934. * ============================================================================= */
  1935. NK_API void nk_property_int(struct nk_context*, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  1936. NK_API void nk_property_float(struct nk_context*, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  1937. NK_API void nk_property_double(struct nk_context*, const char *name, double min, double *val, double max, double step, float inc_per_pixel);
  1938. NK_API int nk_propertyi(struct nk_context*, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  1939. NK_API float nk_propertyf(struct nk_context*, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  1940. NK_API double nk_propertyd(struct nk_context*, const char *name, double min, double val, double max, double step, float inc_per_pixel);
  1941. /* =============================================================================
  1942. *
  1943. * TEXT EDIT
  1944. *
  1945. * ============================================================================= */
  1946. enum nk_edit_flags {
  1947. NK_EDIT_DEFAULT = 0,
  1948. NK_EDIT_READ_ONLY = NK_FLAG(0),
  1949. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  1950. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  1951. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  1952. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  1953. NK_EDIT_SELECTABLE = NK_FLAG(5),
  1954. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  1955. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  1956. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  1957. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  1958. NK_EDIT_MULTILINE = NK_FLAG(10),
  1959. NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
  1960. };
  1961. enum nk_edit_types {
  1962. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  1963. NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE|NK_EDIT_CLIPBOARD,
  1964. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE| NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|NK_EDIT_CLIPBOARD,
  1965. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB| NK_EDIT_CLIPBOARD
  1966. };
  1967. enum nk_edit_events {
  1968. NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
  1969. NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
  1970. NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
  1971. NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
  1972. NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
  1973. };
  1974. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_plugin_filter);
  1975. NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char *buffer, int max, nk_plugin_filter);
  1976. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
  1977. NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
  1978. NK_API void nk_edit_unfocus(struct nk_context*);
  1979. /* =============================================================================
  1980. *
  1981. * CHART
  1982. *
  1983. * ============================================================================= */
  1984. NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  1985. 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);
  1986. NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  1987. 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);
  1988. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  1989. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  1990. NK_API void nk_chart_end(struct nk_context*);
  1991. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
  1992. 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);
  1993. /* =============================================================================
  1994. *
  1995. * POPUP
  1996. *
  1997. * ============================================================================= */
  1998. NK_API int nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  1999. NK_API void nk_popup_close(struct nk_context*);
  2000. NK_API void nk_popup_end(struct nk_context*);
  2001. /* =============================================================================
  2002. *
  2003. * COMBOBOX
  2004. *
  2005. * ============================================================================= */
  2006. NK_API int nk_combo(struct nk_context*, const char **items, int count, int selected, int item_height, struct nk_vec2 size);
  2007. 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);
  2008. 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);
  2009. 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);
  2010. NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
  2011. 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);
  2012. 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);
  2013. 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);
  2014. /* =============================================================================
  2015. *
  2016. * ABSTRACT COMBOBOX
  2017. *
  2018. * ============================================================================= */
  2019. NK_API int nk_combo_begin_text(struct nk_context*, const char *selected, int, struct nk_vec2 size);
  2020. NK_API int nk_combo_begin_label(struct nk_context*, const char *selected, struct nk_vec2 size);
  2021. NK_API int nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
  2022. NK_API int nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
  2023. NK_API int nk_combo_begin_symbol_label(struct nk_context*, const char *selected, enum nk_symbol_type, struct nk_vec2 size);
  2024. NK_API int nk_combo_begin_symbol_text(struct nk_context*, const char *selected, int, enum nk_symbol_type, struct nk_vec2 size);
  2025. NK_API int nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
  2026. NK_API int nk_combo_begin_image_label(struct nk_context*, const char *selected, struct nk_image, struct nk_vec2 size);
  2027. NK_API int nk_combo_begin_image_text(struct nk_context*, const char *selected, int, struct nk_image, struct nk_vec2 size);
  2028. NK_API int nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  2029. NK_API int nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
  2030. NK_API int nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  2031. NK_API int nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
  2032. NK_API int nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  2033. NK_API int nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  2034. NK_API void nk_combo_close(struct nk_context*);
  2035. NK_API void nk_combo_end(struct nk_context*);
  2036. /* =============================================================================
  2037. *
  2038. * CONTEXTUAL
  2039. *
  2040. * ============================================================================= */
  2041. NK_API int nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  2042. NK_API int nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
  2043. NK_API int nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  2044. NK_API int nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  2045. NK_API int nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  2046. NK_API int nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  2047. NK_API int nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  2048. NK_API void nk_contextual_close(struct nk_context*);
  2049. NK_API void nk_contextual_end(struct nk_context*);
  2050. /* =============================================================================
  2051. *
  2052. * TOOLTIP
  2053. *
  2054. * ============================================================================= */
  2055. NK_API void nk_tooltip(struct nk_context*, const char*);
  2056. NK_API int nk_tooltip_begin(struct nk_context*, float width);
  2057. NK_API void nk_tooltip_end(struct nk_context*);
  2058. /* =============================================================================
  2059. *
  2060. * MENU
  2061. *
  2062. * ============================================================================= */
  2063. NK_API void nk_menubar_begin(struct nk_context*);
  2064. NK_API void nk_menubar_end(struct nk_context*);
  2065. NK_API int nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
  2066. NK_API int nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
  2067. NK_API int nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
  2068. NK_API int nk_menu_begin_image_text(struct nk_context*, const char*, int,nk_flags align,struct nk_image, struct nk_vec2 size);
  2069. NK_API int nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align,struct nk_image, struct nk_vec2 size);
  2070. NK_API int nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
  2071. 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);
  2072. NK_API int nk_menu_begin_symbol_label(struct nk_context*, const char*, nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
  2073. NK_API int nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
  2074. NK_API int nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  2075. NK_API int nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  2076. NK_API int nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  2077. NK_API int nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  2078. NK_API int nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  2079. NK_API void nk_menu_close(struct nk_context*);
  2080. NK_API void nk_menu_end(struct nk_context*);
  2081. /* =============================================================================
  2082. *
  2083. * STYLE
  2084. *
  2085. * ============================================================================= */
  2086. enum nk_style_colors {
  2087. NK_COLOR_TEXT,
  2088. NK_COLOR_WINDOW,
  2089. NK_COLOR_HEADER,
  2090. NK_COLOR_BORDER,
  2091. NK_COLOR_BUTTON,
  2092. NK_COLOR_BUTTON_HOVER,
  2093. NK_COLOR_BUTTON_ACTIVE,
  2094. NK_COLOR_TOGGLE,
  2095. NK_COLOR_TOGGLE_HOVER,
  2096. NK_COLOR_TOGGLE_CURSOR,
  2097. NK_COLOR_SELECT,
  2098. NK_COLOR_SELECT_ACTIVE,
  2099. NK_COLOR_SLIDER,
  2100. NK_COLOR_SLIDER_CURSOR,
  2101. NK_COLOR_SLIDER_CURSOR_HOVER,
  2102. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  2103. NK_COLOR_PROPERTY,
  2104. NK_COLOR_EDIT,
  2105. NK_COLOR_EDIT_CURSOR,
  2106. NK_COLOR_COMBO,
  2107. NK_COLOR_CHART,
  2108. NK_COLOR_CHART_COLOR,
  2109. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  2110. NK_COLOR_SCROLLBAR,
  2111. NK_COLOR_SCROLLBAR_CURSOR,
  2112. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  2113. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  2114. NK_COLOR_TAB_HEADER,
  2115. NK_COLOR_COUNT
  2116. };
  2117. enum nk_style_cursor {
  2118. NK_CURSOR_ARROW,
  2119. NK_CURSOR_TEXT,
  2120. NK_CURSOR_MOVE,
  2121. NK_CURSOR_RESIZE_VERTICAL,
  2122. NK_CURSOR_RESIZE_HORIZONTAL,
  2123. NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
  2124. NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
  2125. NK_CURSOR_COUNT
  2126. };
  2127. NK_API void nk_style_default(struct nk_context*);
  2128. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  2129. NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
  2130. NK_API void nk_style_load_all_cursors(struct nk_context*, struct nk_cursor*);
  2131. NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
  2132. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  2133. NK_API int nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
  2134. NK_API void nk_style_show_cursor(struct nk_context*);
  2135. NK_API void nk_style_hide_cursor(struct nk_context*);
  2136. NK_API int nk_style_push_font(struct nk_context*, const struct nk_user_font*);
  2137. NK_API int nk_style_push_float(struct nk_context*, float*, float);
  2138. NK_API int nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
  2139. NK_API int nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
  2140. NK_API int nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
  2141. NK_API int nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
  2142. NK_API int nk_style_pop_font(struct nk_context*);
  2143. NK_API int nk_style_pop_float(struct nk_context*);
  2144. NK_API int nk_style_pop_vec2(struct nk_context*);
  2145. NK_API int nk_style_pop_style_item(struct nk_context*);
  2146. NK_API int nk_style_pop_flags(struct nk_context*);
  2147. NK_API int nk_style_pop_color(struct nk_context*);
  2148. /* =============================================================================
  2149. *
  2150. * COLOR
  2151. *
  2152. * ============================================================================= */
  2153. NK_API struct nk_color nk_rgb(int r, int g, int b);
  2154. NK_API struct nk_color nk_rgb_iv(const int *rgb);
  2155. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  2156. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  2157. NK_API struct nk_color nk_rgb_fv(const float *rgb);
  2158. NK_API struct nk_color nk_rgb_hex(const char *rgb);
  2159. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  2160. NK_API struct nk_color nk_rgba_u32(nk_uint);
  2161. NK_API struct nk_color nk_rgba_iv(const int *rgba);
  2162. NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
  2163. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  2164. NK_API struct nk_color nk_rgba_fv(const float *rgba);
  2165. NK_API struct nk_color nk_rgba_hex(const char *rgb);
  2166. NK_API struct nk_color nk_hsv(int h, int s, int v);
  2167. NK_API struct nk_color nk_hsv_iv(const int *hsv);
  2168. NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
  2169. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  2170. NK_API struct nk_color nk_hsv_fv(const float *hsv);
  2171. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  2172. NK_API struct nk_color nk_hsva_iv(const int *hsva);
  2173. NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
  2174. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  2175. NK_API struct nk_color nk_hsva_fv(const float *hsva);
  2176. /* color (conversion nuklear --> user) */
  2177. NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
  2178. NK_API void nk_color_fv(float *rgba_out, struct nk_color);
  2179. NK_API void nk_color_d(double *r, double *g, double *b, double *a, struct nk_color);
  2180. NK_API void nk_color_dv(double *rgba_out, struct nk_color);
  2181. NK_API nk_uint nk_color_u32(struct nk_color);
  2182. NK_API void nk_color_hex_rgba(char *output, struct nk_color);
  2183. NK_API void nk_color_hex_rgb(char *output, struct nk_color);
  2184. NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
  2185. NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
  2186. NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
  2187. NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
  2188. NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
  2189. NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
  2190. NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
  2191. NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
  2192. NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
  2193. NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
  2194. NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
  2195. NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
  2196. /* =============================================================================
  2197. *
  2198. * IMAGE
  2199. *
  2200. * ============================================================================= */
  2201. NK_API nk_handle nk_handle_ptr(void*);
  2202. NK_API nk_handle nk_handle_id(int);
  2203. NK_API struct nk_image nk_image_handle(nk_handle);
  2204. NK_API struct nk_image nk_image_ptr(void*);
  2205. NK_API struct nk_image nk_image_id(int);
  2206. NK_API int nk_image_is_subimage(const struct nk_image* img);
  2207. NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
  2208. NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
  2209. NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
  2210. /* =============================================================================
  2211. *
  2212. * MATH
  2213. *
  2214. * ============================================================================= */
  2215. NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
  2216. NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  2217. NK_API struct nk_vec2 nk_vec2(float x, float y);
  2218. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  2219. NK_API struct nk_vec2 nk_vec2v(const float *xy);
  2220. NK_API struct nk_vec2 nk_vec2iv(const int *xy);
  2221. NK_API struct nk_rect nk_get_null_rect(void);
  2222. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  2223. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  2224. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  2225. NK_API struct nk_rect nk_rectv(const float *xywh);
  2226. NK_API struct nk_rect nk_rectiv(const int *xywh);
  2227. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  2228. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  2229. /* =============================================================================
  2230. *
  2231. * STRING
  2232. *
  2233. * ============================================================================= */
  2234. NK_API int nk_strlen(const char *str);
  2235. NK_API int nk_stricmp(const char *s1, const char *s2);
  2236. NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
  2237. NK_API int nk_strtoi(const char *str, const char **endptr);
  2238. NK_API float nk_strtof(const char *str, const char **endptr);
  2239. NK_API double nk_strtod(const char *str, const char **endptr);
  2240. NK_API int nk_strfilter(const char *text, const char *regexp);
  2241. NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
  2242. NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
  2243. /* =============================================================================
  2244. *
  2245. * UTF-8
  2246. *
  2247. * ============================================================================= */
  2248. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  2249. NK_API int nk_utf_encode(nk_rune, char*, int);
  2250. NK_API int nk_utf_len(const char*, int byte_len);
  2251. NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
  2252. /* ===============================================================
  2253. *
  2254. * FONT
  2255. *
  2256. * ===============================================================*/
  2257. /* Font handling in this library was designed to be quite customizable and lets
  2258. you decide what you want to use and what you want to provide. There are three
  2259. different ways to use the font atlas. The first two will use your font
  2260. handling scheme and only requires essential data to run nuklear. The next
  2261. slightly more advanced features is font handling with vertex buffer output.
  2262. Finally the most complex API wise is using nuklears font baking API.
  2263. 1.) Using your own implementation without vertex buffer output
  2264. --------------------------------------------------------------
  2265. So first up the easiest way to do font handling is by just providing a
  2266. `nk_user_font` struct which only requires the height in pixel of the used
  2267. font and a callback to calculate the width of a string. This way of handling
  2268. fonts is best fitted for using the normal draw shape command API where you
  2269. do all the text drawing yourself and the library does not require any kind
  2270. of deeper knowledge about which font handling mechanism you use.
  2271. IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
  2272. over the complete life time! I know this sucks but it is currently the only
  2273. way to switch between fonts.
  2274. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  2275. {
  2276. your_font_type *type = handle.ptr;
  2277. float text_width = ...;
  2278. return text_width;
  2279. }
  2280. struct nk_user_font font;
  2281. font.userdata.ptr = &your_font_class_or_struct;
  2282. font.height = your_font_height;
  2283. font.width = your_text_width_calculation;
  2284. struct nk_context ctx;
  2285. nk_init_default(&ctx, &font);
  2286. 2.) Using your own implementation with vertex buffer output
  2287. --------------------------------------------------------------
  2288. While the first approach works fine if you don't want to use the optional
  2289. vertex buffer output it is not enough if you do. To get font handling working
  2290. for these cases you have to provide two additional parameters inside the
  2291. `nk_user_font`. First a texture atlas handle used to draw text as subimages
  2292. of a bigger font atlas texture and a callback to query a character's glyph
  2293. information (offset, size, ...). So it is still possible to provide your own
  2294. font and use the vertex buffer output.
  2295. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  2296. {
  2297. your_font_type *type = handle.ptr;
  2298. float text_width = ...;
  2299. return text_width;
  2300. }
  2301. void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  2302. {
  2303. your_font_type *type = handle.ptr;
  2304. glyph.width = ...;
  2305. glyph.height = ...;
  2306. glyph.xadvance = ...;
  2307. glyph.uv[0].x = ...;
  2308. glyph.uv[0].y = ...;
  2309. glyph.uv[1].x = ...;
  2310. glyph.uv[1].y = ...;
  2311. glyph.offset.x = ...;
  2312. glyph.offset.y = ...;
  2313. }
  2314. struct nk_user_font font;
  2315. font.userdata.ptr = &your_font_class_or_struct;
  2316. font.height = your_font_height;
  2317. font.width = your_text_width_calculation;
  2318. font.query = query_your_font_glyph;
  2319. font.texture.id = your_font_texture;
  2320. struct nk_context ctx;
  2321. nk_init_default(&ctx, &font);
  2322. 3.) Nuklear font baker
  2323. ------------------------------------
  2324. The final approach if you do not have a font handling functionality or don't
  2325. want to use it in this library is by using the optional font baker.
  2326. The font baker API's can be used to create a font plus font atlas texture
  2327. and can be used with or without the vertex buffer output.
  2328. It still uses the `nk_user_font` struct and the two different approaches
  2329. previously stated still work. The font baker is not located inside
  2330. `nk_context` like all other systems since it can be understood as more of
  2331. an extension to nuklear and does not really depend on any `nk_context` state.
  2332. Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
  2333. functions. If you don't care about memory just call the default version
  2334. `nk_font_atlas_init_default` which will allocate all memory from the standard library.
  2335. If you want to control memory allocation but you don't care if the allocated
  2336. memory is temporary and therefore can be freed directly after the baking process
  2337. is over or permanent you can call `nk_font_atlas_init`.
  2338. After successfull intializing the font baker you can add Truetype(.ttf) fonts from
  2339. different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
  2340. functions. Adding font will permanently store each font, font config and ttf memory block(!)
  2341. inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
  2342. the font baker by for example adding additional fonts you can call
  2343. `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
  2344. As soon as you added all fonts you wanted you can now start the baking process
  2345. for every selected glyphes to image by calling `nk_font_atlas_bake`.
  2346. The baking process returns image memory, width and height which can be used to
  2347. either create your own image object or upload it to any graphics library.
  2348. No matter which case you finally have to call `nk_font_atlas_end` which
  2349. will free all temporary memory including the font atlas image so make sure
  2350. you created our texture beforehand. `nk_font_atlas_end` requires a handle
  2351. to your font texture or object and optionally fills a `struct nk_draw_null_texture`
  2352. which can be used for the optional vertex output. If you don't want it just
  2353. set the argument to `NULL`.
  2354. At this point you are done and if you don't want to reuse the font atlas you
  2355. can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
  2356. memory. Finally if you don't use the font atlas and any of it's fonts anymore
  2357. you need to call `nk_font_atlas_clear` to free all memory still being used.
  2358. struct nk_font_atlas atlas;
  2359. nk_font_atlas_init_default(&atlas);
  2360. nk_font_atlas_begin(&atlas);
  2361. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
  2362. nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
  2363. const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
  2364. nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
  2365. struct nk_context ctx;
  2366. nk_init_default(&ctx, &font->handle);
  2367. while (1) {
  2368. }
  2369. nk_font_atlas_clear(&atlas);
  2370. The font baker API is probably the most complex API inside this library and
  2371. I would suggest reading some of my examples `example/` to get a grip on how
  2372. to use the font atlas. There are a number of details I left out. For example
  2373. how to merge fonts, configure a font with `nk_font_config` to use other languages,
  2374. use another texture coodinate format and a lot more:
  2375. struct nk_font_config cfg = nk_font_config(font_pixel_height);
  2376. cfg.merge_mode = nk_false or nk_true;
  2377. cfg.range = nk_font_korean_glyph_ranges();
  2378. cfg.coord_type = NK_COORD_PIXEL;
  2379. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
  2380. */
  2381. struct nk_user_font_glyph;
  2382. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  2383. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  2384. struct nk_user_font_glyph *glyph,
  2385. nk_rune codepoint, nk_rune next_codepoint);
  2386. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  2387. struct nk_user_font_glyph {
  2388. struct nk_vec2 uv[2];
  2389. /* texture coordinates */
  2390. struct nk_vec2 offset;
  2391. /* offset between top left and glyph */
  2392. float width, height;
  2393. /* size of the glyph */
  2394. float xadvance;
  2395. /* offset to the next glyph */
  2396. };
  2397. #endif
  2398. struct nk_user_font {
  2399. nk_handle userdata;
  2400. /* user provided font handle */
  2401. float height;
  2402. /* max height of the font */
  2403. nk_text_width_f width;
  2404. /* font string width in pixel callback */
  2405. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  2406. nk_query_font_glyph_f query;
  2407. /* font glyph callback to query drawing info */
  2408. nk_handle texture;
  2409. /* texture handle to the used font atlas or texture */
  2410. #endif
  2411. };
  2412. #ifdef NK_INCLUDE_FONT_BAKING
  2413. enum nk_font_coord_type {
  2414. NK_COORD_UV, /* texture coordinates inside font glyphs are clamped between 0-1 */
  2415. NK_COORD_PIXEL /* texture coordinates inside font glyphs are in absolute pixel */
  2416. };
  2417. struct nk_baked_font {
  2418. float height;
  2419. /* height of the font */
  2420. float ascent, descent;
  2421. /* font glyphs ascent and descent */
  2422. nk_rune glyph_offset;
  2423. /* glyph array offset inside the font glyph baking output array */
  2424. nk_rune glyph_count;
  2425. /* number of glyphs of this font inside the glyph baking array output */
  2426. const nk_rune *ranges;
  2427. /* font codepoint ranges as pairs of (from/to) and 0 as last element */
  2428. };
  2429. struct nk_font_config {
  2430. struct nk_font_config *next;
  2431. /* NOTE: only used internally */
  2432. void *ttf_blob;
  2433. /* pointer to loaded TTF file memory block.
  2434. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  2435. nk_size ttf_size;
  2436. /* size of the loaded TTF file memory block
  2437. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  2438. unsigned char ttf_data_owned_by_atlas;
  2439. /* used inside font atlas: default to: 0*/
  2440. unsigned char merge_mode;
  2441. /* merges this font into the last font */
  2442. unsigned char pixel_snap;
  2443. /* align every character to pixel boundary (if true set oversample (1,1)) */
  2444. unsigned char oversample_v, oversample_h;
  2445. /* rasterize at hight quality for sub-pixel position */
  2446. unsigned char padding[3];
  2447. float size;
  2448. /* baked pixel height of the font */
  2449. enum nk_font_coord_type coord_type;
  2450. /* texture coordinate format with either pixel or UV coordinates */
  2451. struct nk_vec2 spacing;
  2452. /* extra pixel spacing between glyphs */
  2453. const nk_rune *range;
  2454. /* list of unicode ranges (2 values per range, zero terminated) */
  2455. struct nk_baked_font *font;
  2456. /* font to setup in the baking process: NOTE: not needed for font atlas */
  2457. nk_rune fallback_glyph;
  2458. /* fallback glyph to use if a given rune is not found */
  2459. };
  2460. struct nk_font_glyph {
  2461. nk_rune codepoint;
  2462. float xadvance;
  2463. float x0, y0, x1, y1, w, h;
  2464. float u0, v0, u1, v1;
  2465. };
  2466. struct nk_font {
  2467. struct nk_font *next;
  2468. struct nk_user_font handle;
  2469. struct nk_baked_font info;
  2470. float scale;
  2471. struct nk_font_glyph *glyphs;
  2472. const struct nk_font_glyph *fallback;
  2473. nk_rune fallback_codepoint;
  2474. nk_handle texture;
  2475. struct nk_font_config *config;
  2476. };
  2477. enum nk_font_atlas_format {
  2478. NK_FONT_ATLAS_ALPHA8,
  2479. NK_FONT_ATLAS_RGBA32
  2480. };
  2481. struct nk_font_atlas {
  2482. void *pixel;
  2483. int tex_width;
  2484. int tex_height;
  2485. struct nk_allocator permanent;
  2486. struct nk_allocator temporary;
  2487. struct nk_recti custom;
  2488. struct nk_cursor cursors[NK_CURSOR_COUNT];
  2489. int glyph_count;
  2490. struct nk_font_glyph *glyphs;
  2491. struct nk_font *default_font;
  2492. struct nk_font *fonts;
  2493. struct nk_font_config *config;
  2494. int font_num;
  2495. };
  2496. /* some language glyph codepoint ranges */
  2497. NK_API const nk_rune *nk_font_default_glyph_ranges(void);
  2498. NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
  2499. NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
  2500. NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
  2501. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  2502. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  2503. #endif
  2504. NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
  2505. NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, struct nk_allocator *persistent, struct nk_allocator *transient);
  2506. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  2507. NK_API struct nk_font_config nk_font_config(float pixel_height);
  2508. NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  2509. #ifdef NK_INCLUDE_DEFAULT_FONT
  2510. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  2511. #endif
  2512. 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);
  2513. #ifdef NK_INCLUDE_STANDARD_IO
  2514. 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*);
  2515. #endif
  2516. 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*);
  2517. 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);
  2518. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
  2519. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  2520. NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
  2521. NK_API void nk_font_atlas_cleanup(struct nk_font_atlas *atlas);
  2522. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  2523. #endif
  2524. /* ==============================================================
  2525. *
  2526. * MEMORY BUFFER
  2527. *
  2528. * ===============================================================*/
  2529. /* A basic (double)-buffer with linear allocation and resetting as only
  2530. freeing policy. The buffer's main purpose is to control all memory management
  2531. inside the GUI toolkit and still leave memory control as much as possible in
  2532. the hand of the user while also making sure the library is easy to use if
  2533. not as much control is needed.
  2534. In general all memory inside this library can be provided from the user in
  2535. three different ways.
  2536. The first way and the one providing most control is by just passing a fixed
  2537. size memory block. In this case all control lies in the hand of the user
  2538. since he can exactly control where the memory comes from and how much memory
  2539. the library should consume. Of course using the fixed size API removes the
  2540. ability to automatically resize a buffer if not enough memory is provided so
  2541. you have to take over the resizing. While being a fixed sized buffer sounds
  2542. quite limiting, it is very effective in this library since the actual memory
  2543. consumption is quite stable and has a fixed upper bound for a lot of cases.
  2544. If you don't want to think about how much memory the library should allocate
  2545. at all time or have a very dynamic UI with unpredictable memory consumption
  2546. habits but still want control over memory allocation you can use the dynamic
  2547. allocator based API. The allocator consists of two callbacks for allocating
  2548. and freeing memory and optional userdata so you can plugin your own allocator.
  2549. The final and easiest way can be used by defining
  2550. NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  2551. allocation functions malloc and free and takes over complete control over
  2552. memory in this library.
  2553. */
  2554. struct nk_memory_status {
  2555. void *memory;
  2556. unsigned int type;
  2557. nk_size size;
  2558. nk_size allocated;
  2559. nk_size needed;
  2560. nk_size calls;
  2561. };
  2562. enum nk_allocation_type {
  2563. NK_BUFFER_FIXED,
  2564. NK_BUFFER_DYNAMIC
  2565. };
  2566. enum nk_buffer_allocation_type {
  2567. NK_BUFFER_FRONT,
  2568. NK_BUFFER_BACK,
  2569. NK_BUFFER_MAX
  2570. };
  2571. struct nk_buffer_marker {
  2572. int active;
  2573. nk_size offset;
  2574. };
  2575. struct nk_memory {void *ptr;nk_size size;};
  2576. struct nk_buffer {
  2577. struct nk_buffer_marker marker[NK_BUFFER_MAX];
  2578. /* buffer marker to free a buffer to a certain offset */
  2579. struct nk_allocator pool;
  2580. /* allocator callback for dynamic buffers */
  2581. enum nk_allocation_type type;
  2582. /* memory management type */
  2583. struct nk_memory memory;
  2584. /* memory and size of the current memory block */
  2585. float grow_factor;
  2586. /* growing factor for dynamic memory management */
  2587. nk_size allocated;
  2588. /* total amount of memory allocated */
  2589. nk_size needed;
  2590. /* totally consumed memory given that enough memory is present */
  2591. nk_size calls;
  2592. /* number of allocation calls */
  2593. nk_size size;
  2594. /* current size of the buffer */
  2595. };
  2596. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  2597. NK_API void nk_buffer_init_default(struct nk_buffer*);
  2598. #endif
  2599. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  2600. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
  2601. NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
  2602. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void *memory, nk_size size, nk_size align);
  2603. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  2604. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  2605. NK_API void nk_buffer_clear(struct nk_buffer*);
  2606. NK_API void nk_buffer_free(struct nk_buffer*);
  2607. NK_API void *nk_buffer_memory(struct nk_buffer*);
  2608. NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
  2609. NK_API nk_size nk_buffer_total(struct nk_buffer*);
  2610. /* ==============================================================
  2611. *
  2612. * STRING
  2613. *
  2614. * ===============================================================*/
  2615. /* Basic string buffer which is only used in context with the text editor
  2616. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  2617. * the default string handling method. The only instance you should have any contact
  2618. * with this API is if you interact with an `nk_text_edit` object inside one of the
  2619. * copy and paste functions and even there only for more advanced cases. */
  2620. struct nk_str {
  2621. struct nk_buffer buffer;
  2622. int len; /* in codepoints/runes/glyphs */
  2623. };
  2624. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  2625. NK_API void nk_str_init_default(struct nk_str*);
  2626. #endif
  2627. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  2628. NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
  2629. NK_API void nk_str_clear(struct nk_str*);
  2630. NK_API void nk_str_free(struct nk_str*);
  2631. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  2632. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  2633. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  2634. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  2635. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  2636. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  2637. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  2638. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  2639. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  2640. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  2641. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  2642. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  2643. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  2644. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  2645. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  2646. NK_API void nk_str_remove_runes(struct nk_str *str, int len);
  2647. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  2648. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  2649. NK_API char *nk_str_at_char(struct nk_str*, int pos);
  2650. NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
  2651. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  2652. NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
  2653. NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
  2654. NK_API char *nk_str_get(struct nk_str*);
  2655. NK_API const char *nk_str_get_const(const struct nk_str*);
  2656. NK_API int nk_str_len(struct nk_str*);
  2657. NK_API int nk_str_len_char(struct nk_str*);
  2658. /*===============================================================
  2659. *
  2660. * TEXT EDITOR
  2661. *
  2662. * ===============================================================*/
  2663. /* Editing text in this library is handled by either `nk_edit_string` or
  2664. * `nk_edit_buffer`. But like almost everything in this library there are multiple
  2665. * ways of doing it and a balance between control and ease of use with memory
  2666. * as well as functionality controlled by flags.
  2667. *
  2668. * This library generally allows three different levels of memory control:
  2669. * First of is the most basic way of just providing a simple char array with
  2670. * string length. This method is probably the easiest way of handling simple
  2671. * user text input. Main upside is complete control over memory while the biggest
  2672. * downside in comparsion with the other two approaches is missing undo/redo.
  2673. *
  2674. * For UIs that require undo/redo the second way was created. It is based on
  2675. * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  2676. * This is mainly useful if you want something more like a text editor but don't want
  2677. * to have a dynamically growing buffer.
  2678. *
  2679. * The final way is using a dynamically growing nk_text_edit struct, which
  2680. * has both a default version if you don't care where memory comes from and an
  2681. * allocator version if you do. While the text editor is quite powerful for its
  2682. * complexity I would not recommend editing gigabytes of data with it.
  2683. * It is rather designed for uses cases which make sense for a GUI library not for
  2684. * an full blown text editor.
  2685. */
  2686. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  2687. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  2688. #endif
  2689. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  2690. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  2691. #endif
  2692. struct nk_text_edit;
  2693. struct nk_clipboard {
  2694. nk_handle userdata;
  2695. nk_plugin_paste paste;
  2696. nk_plugin_copy copy;
  2697. };
  2698. struct nk_text_undo_record {
  2699. int where;
  2700. short insert_length;
  2701. short delete_length;
  2702. short char_storage;
  2703. };
  2704. struct nk_text_undo_state {
  2705. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  2706. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  2707. short undo_point;
  2708. short redo_point;
  2709. short undo_char_point;
  2710. short redo_char_point;
  2711. };
  2712. enum nk_text_edit_type {
  2713. NK_TEXT_EDIT_SINGLE_LINE,
  2714. NK_TEXT_EDIT_MULTI_LINE
  2715. };
  2716. enum nk_text_edit_mode {
  2717. NK_TEXT_EDIT_MODE_VIEW,
  2718. NK_TEXT_EDIT_MODE_INSERT,
  2719. NK_TEXT_EDIT_MODE_REPLACE
  2720. };
  2721. struct nk_text_edit {
  2722. struct nk_clipboard clip;
  2723. struct nk_str string;
  2724. nk_plugin_filter filter;
  2725. struct nk_vec2 scrollbar;
  2726. int cursor;
  2727. int select_start;
  2728. int select_end;
  2729. unsigned char mode;
  2730. unsigned char cursor_at_end_of_line;
  2731. unsigned char initialized;
  2732. unsigned char has_preferred_x;
  2733. unsigned char single_line;
  2734. unsigned char active;
  2735. unsigned char padding1;
  2736. float preferred_x;
  2737. struct nk_text_undo_state undo;
  2738. };
  2739. /* filter function */
  2740. NK_API int nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  2741. NK_API int nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  2742. NK_API int nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  2743. NK_API int nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  2744. NK_API int nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  2745. NK_API int nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  2746. NK_API int nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  2747. /* text editor */
  2748. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  2749. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  2750. #endif
  2751. NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
  2752. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
  2753. NK_API void nk_textedit_free(struct nk_text_edit*);
  2754. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  2755. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  2756. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  2757. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  2758. NK_API int nk_textedit_cut(struct nk_text_edit*);
  2759. NK_API int nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  2760. NK_API void nk_textedit_undo(struct nk_text_edit*);
  2761. NK_API void nk_textedit_redo(struct nk_text_edit*);
  2762. /* ===============================================================
  2763. *
  2764. * DRAWING
  2765. *
  2766. * ===============================================================*/
  2767. /* This library was designed to be render backend agnostic so it does
  2768. not draw anything to screen. Instead all drawn shapes, widgets
  2769. are made of, are buffered into memory and make up a command queue.
  2770. Each frame therefore fills the command buffer with draw commands
  2771. that then need to be executed by the user and his own render backend.
  2772. After that the command buffer needs to be cleared and a new frame can be
  2773. started. It is probably important to note that the command buffer is the main
  2774. drawing API and the optional vertex buffer API only takes this format and
  2775. converts it into a hardware accessible format.
  2776. To use the command queue to draw your own widgets you can access the
  2777. command buffer of each window by calling `nk_window_get_canvas` after
  2778. previously having called `nk_begin`:
  2779. void draw_red_rectangle_widget(struct nk_context *ctx)
  2780. {
  2781. struct nk_command_buffer *canvas;
  2782. struct nk_input *input = &ctx->input;
  2783. canvas = nk_window_get_canvas(ctx);
  2784. struct nk_rect space;
  2785. enum nk_widget_layout_states state;
  2786. state = nk_widget(&space, ctx);
  2787. if (!state) return;
  2788. if (state != NK_WIDGET_ROM)
  2789. update_your_widget_by_user_input(...);
  2790. nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
  2791. }
  2792. if (nk_begin(...)) {
  2793. nk_layout_row_dynamic(ctx, 25, 1);
  2794. draw_red_rectangle_widget(ctx);
  2795. }
  2796. nk_end(..)
  2797. Important to know if you want to create your own widgets is the `nk_widget`
  2798. call. It allocates space on the panel reserved for this widget to be used,
  2799. but also returns the state of the widget space. If your widget is not seen and does
  2800. not have to be updated it is '0' and you can just return. If it only has
  2801. to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
  2802. update and draw your widget. The reason for seperating is to only draw and
  2803. update what is actually neccessary which is crucial for performance.
  2804. */
  2805. enum nk_command_type {
  2806. NK_COMMAND_NOP,
  2807. NK_COMMAND_SCISSOR,
  2808. NK_COMMAND_LINE,
  2809. NK_COMMAND_CURVE,
  2810. NK_COMMAND_RECT,
  2811. NK_COMMAND_RECT_FILLED,
  2812. NK_COMMAND_RECT_MULTI_COLOR,
  2813. NK_COMMAND_CIRCLE,
  2814. NK_COMMAND_CIRCLE_FILLED,
  2815. NK_COMMAND_ARC,
  2816. NK_COMMAND_ARC_FILLED,
  2817. NK_COMMAND_TRIANGLE,
  2818. NK_COMMAND_TRIANGLE_FILLED,
  2819. NK_COMMAND_POLYGON,
  2820. NK_COMMAND_POLYGON_FILLED,
  2821. NK_COMMAND_POLYLINE,
  2822. NK_COMMAND_TEXT,
  2823. NK_COMMAND_IMAGE,
  2824. NK_COMMAND_CUSTOM
  2825. };
  2826. /* command base and header of every command inside the buffer */
  2827. struct nk_command {
  2828. enum nk_command_type type;
  2829. nk_size next;
  2830. #ifdef NK_INCLUDE_COMMAND_USERDATA
  2831. nk_handle userdata;
  2832. #endif
  2833. };
  2834. struct nk_command_scissor {
  2835. struct nk_command header;
  2836. short x, y;
  2837. unsigned short w, h;
  2838. };
  2839. struct nk_command_line {
  2840. struct nk_command header;
  2841. unsigned short line_thickness;
  2842. struct nk_vec2i begin;
  2843. struct nk_vec2i end;
  2844. struct nk_color color;
  2845. };
  2846. struct nk_command_curve {
  2847. struct nk_command header;
  2848. unsigned short line_thickness;
  2849. struct nk_vec2i begin;
  2850. struct nk_vec2i end;
  2851. struct nk_vec2i ctrl[2];
  2852. struct nk_color color;
  2853. };
  2854. struct nk_command_rect {
  2855. struct nk_command header;
  2856. unsigned short rounding;
  2857. unsigned short line_thickness;
  2858. short x, y;
  2859. unsigned short w, h;
  2860. struct nk_color color;
  2861. };
  2862. struct nk_command_rect_filled {
  2863. struct nk_command header;
  2864. unsigned short rounding;
  2865. short x, y;
  2866. unsigned short w, h;
  2867. struct nk_color color;
  2868. };
  2869. struct nk_command_rect_multi_color {
  2870. struct nk_command header;
  2871. short x, y;
  2872. unsigned short w, h;
  2873. struct nk_color left;
  2874. struct nk_color top;
  2875. struct nk_color bottom;
  2876. struct nk_color right;
  2877. };
  2878. struct nk_command_triangle {
  2879. struct nk_command header;
  2880. unsigned short line_thickness;
  2881. struct nk_vec2i a;
  2882. struct nk_vec2i b;
  2883. struct nk_vec2i c;
  2884. struct nk_color color;
  2885. };
  2886. struct nk_command_triangle_filled {
  2887. struct nk_command header;
  2888. struct nk_vec2i a;
  2889. struct nk_vec2i b;
  2890. struct nk_vec2i c;
  2891. struct nk_color color;
  2892. };
  2893. struct nk_command_circle {
  2894. struct nk_command header;
  2895. short x, y;
  2896. unsigned short line_thickness;
  2897. unsigned short w, h;
  2898. struct nk_color color;
  2899. };
  2900. struct nk_command_circle_filled {
  2901. struct nk_command header;
  2902. short x, y;
  2903. unsigned short w, h;
  2904. struct nk_color color;
  2905. };
  2906. struct nk_command_arc {
  2907. struct nk_command header;
  2908. short cx, cy;
  2909. unsigned short r;
  2910. unsigned short line_thickness;
  2911. float a[2];
  2912. struct nk_color color;
  2913. };
  2914. struct nk_command_arc_filled {
  2915. struct nk_command header;
  2916. short cx, cy;
  2917. unsigned short r;
  2918. float a[2];
  2919. struct nk_color color;
  2920. };
  2921. struct nk_command_polygon {
  2922. struct nk_command header;
  2923. struct nk_color color;
  2924. unsigned short line_thickness;
  2925. unsigned short point_count;
  2926. struct nk_vec2i points[1];
  2927. };
  2928. struct nk_command_polygon_filled {
  2929. struct nk_command header;
  2930. struct nk_color color;
  2931. unsigned short point_count;
  2932. struct nk_vec2i points[1];
  2933. };
  2934. struct nk_command_polyline {
  2935. struct nk_command header;
  2936. struct nk_color color;
  2937. unsigned short line_thickness;
  2938. unsigned short point_count;
  2939. struct nk_vec2i points[1];
  2940. };
  2941. struct nk_command_image {
  2942. struct nk_command header;
  2943. short x, y;
  2944. unsigned short w, h;
  2945. struct nk_image img;
  2946. struct nk_color col;
  2947. };
  2948. typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
  2949. unsigned short w, unsigned short h, nk_handle callback_data);
  2950. struct nk_command_custom {
  2951. struct nk_command header;
  2952. short x, y;
  2953. unsigned short w, h;
  2954. nk_handle callback_data;
  2955. nk_command_custom_callback callback;
  2956. };
  2957. struct nk_command_text {
  2958. struct nk_command header;
  2959. const struct nk_user_font *font;
  2960. struct nk_color background;
  2961. struct nk_color foreground;
  2962. short x, y;
  2963. unsigned short w, h;
  2964. float height;
  2965. int length;
  2966. char string[1];
  2967. };
  2968. enum nk_command_clipping {
  2969. NK_CLIPPING_OFF = nk_false,
  2970. NK_CLIPPING_ON = nk_true
  2971. };
  2972. struct nk_command_buffer {
  2973. struct nk_buffer *base;
  2974. struct nk_rect clip;
  2975. int use_clipping;
  2976. nk_handle userdata;
  2977. nk_size begin, end, last;
  2978. };
  2979. /* shape outlines */
  2980. 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);
  2981. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  2982. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  2983. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  2984. 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);
  2985. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  2986. NK_API void nk_stroke_polyline(struct nk_command_buffer*, float *points, int point_count, float line_thickness, struct nk_color col);
  2987. NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
  2988. /* filled shades */
  2989. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  2990. 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);
  2991. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  2992. 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);
  2993. 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);
  2994. NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
  2995. /* misc */
  2996. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
  2997. 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);
  2998. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  2999. NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
  3000. /* ===============================================================
  3001. *
  3002. * INPUT
  3003. *
  3004. * ===============================================================*/
  3005. struct nk_mouse_button {
  3006. int down;
  3007. unsigned int clicked;
  3008. struct nk_vec2 clicked_pos;
  3009. };
  3010. struct nk_mouse {
  3011. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  3012. struct nk_vec2 pos;
  3013. struct nk_vec2 prev;
  3014. struct nk_vec2 delta;
  3015. struct nk_vec2 scroll_delta;
  3016. unsigned char grab;
  3017. unsigned char grabbed;
  3018. unsigned char ungrab;
  3019. };
  3020. struct nk_key {
  3021. int down;
  3022. unsigned int clicked;
  3023. };
  3024. struct nk_keyboard {
  3025. struct nk_key keys[NK_KEY_MAX];
  3026. char text[NK_INPUT_MAX];
  3027. int text_len;
  3028. };
  3029. struct nk_input {
  3030. struct nk_keyboard keyboard;
  3031. struct nk_mouse mouse;
  3032. };
  3033. NK_API int nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  3034. NK_API int nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  3035. NK_API int nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, int down);
  3036. NK_API int nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  3037. 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);
  3038. NK_API int nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  3039. NK_API int nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  3040. NK_API int nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  3041. NK_API int nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  3042. NK_API int nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  3043. NK_API int nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  3044. NK_API int nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  3045. NK_API int nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  3046. NK_API int nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  3047. NK_API int nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  3048. /* ===============================================================
  3049. *
  3050. * DRAW LIST
  3051. *
  3052. * ===============================================================*/
  3053. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  3054. /* The optional vertex buffer draw list provides a 2D drawing context
  3055. with antialiasing functionality which takes basic filled or outlined shapes
  3056. or a path and outputs vertexes, elements and draw commands.
  3057. The actual draw list API is not required to be used directly while using this
  3058. library since converting the default library draw command output is done by
  3059. just calling `nk_convert` but I decided to still make this library accessible
  3060. since it can be useful.
  3061. The draw list is based on a path buffering and polygon and polyline
  3062. rendering API which allows a lot of ways to draw 2D content to screen.
  3063. In fact it is probably more powerful than needed but allows even more crazy
  3064. things than this library provides by default.
  3065. */
  3066. typedef nk_ushort nk_draw_index;
  3067. enum nk_draw_list_stroke {
  3068. NK_STROKE_OPEN = nk_false,
  3069. /* build up path has no connection back to the beginning */
  3070. NK_STROKE_CLOSED = nk_true
  3071. /* build up path has a connection back to the beginning */
  3072. };
  3073. enum nk_draw_vertex_layout_attribute {
  3074. NK_VERTEX_POSITION,
  3075. NK_VERTEX_COLOR,
  3076. NK_VERTEX_TEXCOORD,
  3077. NK_VERTEX_ATTRIBUTE_COUNT
  3078. };
  3079. enum nk_draw_vertex_layout_format {
  3080. NK_FORMAT_SCHAR,
  3081. NK_FORMAT_SSHORT,
  3082. NK_FORMAT_SINT,
  3083. NK_FORMAT_UCHAR,
  3084. NK_FORMAT_USHORT,
  3085. NK_FORMAT_UINT,
  3086. NK_FORMAT_FLOAT,
  3087. NK_FORMAT_DOUBLE,
  3088. NK_FORMAT_COLOR_BEGIN,
  3089. NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
  3090. NK_FORMAT_R16G15B16,
  3091. NK_FORMAT_R32G32B32,
  3092. NK_FORMAT_R8G8B8A8,
  3093. NK_FORMAT_B8G8R8A8,
  3094. NK_FORMAT_R16G15B16A16,
  3095. NK_FORMAT_R32G32B32A32,
  3096. NK_FORMAT_R32G32B32A32_FLOAT,
  3097. NK_FORMAT_R32G32B32A32_DOUBLE,
  3098. NK_FORMAT_RGB32,
  3099. NK_FORMAT_RGBA32,
  3100. NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
  3101. NK_FORMAT_COUNT
  3102. };
  3103. #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
  3104. struct nk_draw_vertex_layout_element {
  3105. enum nk_draw_vertex_layout_attribute attribute;
  3106. enum nk_draw_vertex_layout_format format;
  3107. nk_size offset;
  3108. };
  3109. struct nk_draw_command {
  3110. unsigned int elem_count;
  3111. /* number of elements in the current draw batch */
  3112. struct nk_rect clip_rect;
  3113. /* current screen clipping rectangle */
  3114. nk_handle texture;
  3115. /* current texture to set */
  3116. #ifdef NK_INCLUDE_COMMAND_USERDATA
  3117. nk_handle userdata;
  3118. #endif
  3119. };
  3120. struct nk_draw_list {
  3121. struct nk_rect clip_rect;
  3122. struct nk_vec2 circle_vtx[12];
  3123. struct nk_convert_config config;
  3124. struct nk_buffer *buffer;
  3125. struct nk_buffer *vertices;
  3126. struct nk_buffer *elements;
  3127. unsigned int element_count;
  3128. unsigned int vertex_count;
  3129. unsigned int cmd_count;
  3130. nk_size cmd_offset;
  3131. unsigned int path_count;
  3132. unsigned int path_offset;
  3133. enum nk_anti_aliasing line_AA;
  3134. enum nk_anti_aliasing shape_AA;
  3135. #ifdef NK_INCLUDE_COMMAND_USERDATA
  3136. nk_handle userdata;
  3137. #endif
  3138. };
  3139. /* draw list */
  3140. NK_API void nk_draw_list_init(struct nk_draw_list*);
  3141. 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);
  3142. NK_API void nk_draw_list_clear(struct nk_draw_list*);
  3143. /* drawing */
  3144. #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))
  3145. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  3146. 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*);
  3147. NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
  3148. NK_API void nk_draw_list_clear(struct nk_draw_list *list);
  3149. /* path */
  3150. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  3151. NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
  3152. 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);
  3153. 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);
  3154. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  3155. 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);
  3156. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  3157. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  3158. /* stroke */
  3159. 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);
  3160. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  3161. 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);
  3162. 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);
  3163. 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);
  3164. 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);
  3165. /* fill */
  3166. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  3167. 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);
  3168. 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);
  3169. 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);
  3170. 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);
  3171. /* misc */
  3172. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  3173. 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);
  3174. #ifdef NK_INCLUDE_COMMAND_USERDATA
  3175. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  3176. #endif
  3177. #endif
  3178. /* ===============================================================
  3179. *
  3180. * GUI
  3181. *
  3182. * ===============================================================*/
  3183. enum nk_style_item_type {
  3184. NK_STYLE_ITEM_COLOR,
  3185. NK_STYLE_ITEM_IMAGE
  3186. };
  3187. union nk_style_item_data {
  3188. struct nk_image image;
  3189. struct nk_color color;
  3190. };
  3191. struct nk_style_item {
  3192. enum nk_style_item_type type;
  3193. union nk_style_item_data data;
  3194. };
  3195. struct nk_style_text {
  3196. struct nk_color color;
  3197. struct nk_vec2 padding;
  3198. };
  3199. struct nk_style_button {
  3200. /* background */
  3201. struct nk_style_item normal;
  3202. struct nk_style_item hover;
  3203. struct nk_style_item active;
  3204. struct nk_color border_color;
  3205. /* text */
  3206. struct nk_color text_background;
  3207. struct nk_color text_normal;
  3208. struct nk_color text_hover;
  3209. struct nk_color text_active;
  3210. nk_flags text_alignment;
  3211. /* properties */
  3212. float border;
  3213. float rounding;
  3214. struct nk_vec2 padding;
  3215. struct nk_vec2 image_padding;
  3216. struct nk_vec2 touch_padding;
  3217. /* optional user callbacks */
  3218. nk_handle userdata;
  3219. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  3220. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  3221. };
  3222. struct nk_style_toggle {
  3223. /* background */
  3224. struct nk_style_item normal;
  3225. struct nk_style_item hover;
  3226. struct nk_style_item active;
  3227. struct nk_color border_color;
  3228. /* cursor */
  3229. struct nk_style_item cursor_normal;
  3230. struct nk_style_item cursor_hover;
  3231. /* text */
  3232. struct nk_color text_normal;
  3233. struct nk_color text_hover;
  3234. struct nk_color text_active;
  3235. struct nk_color text_background;
  3236. nk_flags text_alignment;
  3237. /* properties */
  3238. struct nk_vec2 padding;
  3239. struct nk_vec2 touch_padding;
  3240. float spacing;
  3241. float border;
  3242. /* optional user callbacks */
  3243. nk_handle userdata;
  3244. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  3245. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  3246. };
  3247. struct nk_style_selectable {
  3248. /* background (inactive) */
  3249. struct nk_style_item normal;
  3250. struct nk_style_item hover;
  3251. struct nk_style_item pressed;
  3252. /* background (active) */
  3253. struct nk_style_item normal_active;
  3254. struct nk_style_item hover_active;
  3255. struct nk_style_item pressed_active;
  3256. /* text color (inactive) */
  3257. struct nk_color text_normal;
  3258. struct nk_color text_hover;
  3259. struct nk_color text_pressed;
  3260. /* text color (active) */
  3261. struct nk_color text_normal_active;
  3262. struct nk_color text_hover_active;
  3263. struct nk_color text_pressed_active;
  3264. struct nk_color text_background;
  3265. nk_flags text_alignment;
  3266. /* properties */
  3267. float rounding;
  3268. struct nk_vec2 padding;
  3269. struct nk_vec2 touch_padding;
  3270. struct nk_vec2 image_padding;
  3271. /* optional user callbacks */
  3272. nk_handle userdata;
  3273. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  3274. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  3275. };
  3276. struct nk_style_slider {
  3277. /* background */
  3278. struct nk_style_item normal;
  3279. struct nk_style_item hover;
  3280. struct nk_style_item active;
  3281. struct nk_color border_color;
  3282. /* background bar */
  3283. struct nk_color bar_normal;
  3284. struct nk_color bar_hover;
  3285. struct nk_color bar_active;
  3286. struct nk_color bar_filled;
  3287. /* cursor */
  3288. struct nk_style_item cursor_normal;
  3289. struct nk_style_item cursor_hover;
  3290. struct nk_style_item cursor_active;
  3291. /* properties */
  3292. float border;
  3293. float rounding;
  3294. float bar_height;
  3295. struct nk_vec2 padding;
  3296. struct nk_vec2 spacing;
  3297. struct nk_vec2 cursor_size;
  3298. /* optional buttons */
  3299. int show_buttons;
  3300. struct nk_style_button inc_button;
  3301. struct nk_style_button dec_button;
  3302. enum nk_symbol_type inc_symbol;
  3303. enum nk_symbol_type dec_symbol;
  3304. /* optional user callbacks */
  3305. nk_handle userdata;
  3306. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  3307. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  3308. };
  3309. struct nk_style_progress {
  3310. /* background */
  3311. struct nk_style_item normal;
  3312. struct nk_style_item hover;
  3313. struct nk_style_item active;
  3314. struct nk_color border_color;
  3315. /* cursor */
  3316. struct nk_style_item cursor_normal;
  3317. struct nk_style_item cursor_hover;
  3318. struct nk_style_item cursor_active;
  3319. struct nk_color cursor_border_color;
  3320. /* properties */
  3321. float rounding;
  3322. float border;
  3323. float cursor_border;
  3324. float cursor_rounding;
  3325. struct nk_vec2 padding;
  3326. /* optional user callbacks */
  3327. nk_handle userdata;
  3328. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  3329. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  3330. };
  3331. struct nk_style_scrollbar {
  3332. /* background */
  3333. struct nk_style_item normal;
  3334. struct nk_style_item hover;
  3335. struct nk_style_item active;
  3336. struct nk_color border_color;
  3337. /* cursor */
  3338. struct nk_style_item cursor_normal;
  3339. struct nk_style_item cursor_hover;
  3340. struct nk_style_item cursor_active;
  3341. struct nk_color cursor_border_color;
  3342. /* properties */
  3343. float border;
  3344. float rounding;
  3345. float border_cursor;
  3346. float rounding_cursor;
  3347. struct nk_vec2 padding;
  3348. /* optional buttons */
  3349. int show_buttons;
  3350. struct nk_style_button inc_button;
  3351. struct nk_style_button dec_button;
  3352. enum nk_symbol_type inc_symbol;
  3353. enum nk_symbol_type dec_symbol;
  3354. /* optional user callbacks */
  3355. nk_handle userdata;
  3356. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  3357. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  3358. };
  3359. struct nk_style_edit {
  3360. /* background */
  3361. struct nk_style_item normal;
  3362. struct nk_style_item hover;
  3363. struct nk_style_item active;
  3364. struct nk_color border_color;
  3365. struct nk_style_scrollbar scrollbar;
  3366. /* cursor */
  3367. struct nk_color cursor_normal;
  3368. struct nk_color cursor_hover;
  3369. struct nk_color cursor_text_normal;
  3370. struct nk_color cursor_text_hover;
  3371. /* text (unselected) */
  3372. struct nk_color text_normal;
  3373. struct nk_color text_hover;
  3374. struct nk_color text_active;
  3375. /* text (selected) */
  3376. struct nk_color selected_normal;
  3377. struct nk_color selected_hover;
  3378. struct nk_color selected_text_normal;
  3379. struct nk_color selected_text_hover;
  3380. /* properties */
  3381. float border;
  3382. float rounding;
  3383. float cursor_size;
  3384. struct nk_vec2 scrollbar_size;
  3385. struct nk_vec2 padding;
  3386. float row_padding;
  3387. };
  3388. struct nk_style_property {
  3389. /* background */
  3390. struct nk_style_item normal;
  3391. struct nk_style_item hover;
  3392. struct nk_style_item active;
  3393. struct nk_color border_color;
  3394. /* text */
  3395. struct nk_color label_normal;
  3396. struct nk_color label_hover;
  3397. struct nk_color label_active;
  3398. /* symbols */
  3399. enum nk_symbol_type sym_left;
  3400. enum nk_symbol_type sym_right;
  3401. /* properties */
  3402. float border;
  3403. float rounding;
  3404. struct nk_vec2 padding;
  3405. struct nk_style_edit edit;
  3406. struct nk_style_button inc_button;
  3407. struct nk_style_button dec_button;
  3408. /* optional user callbacks */
  3409. nk_handle userdata;
  3410. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  3411. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  3412. };
  3413. struct nk_style_chart {
  3414. /* colors */
  3415. struct nk_style_item background;
  3416. struct nk_color border_color;
  3417. struct nk_color selected_color;
  3418. struct nk_color color;
  3419. /* properties */
  3420. float border;
  3421. float rounding;
  3422. struct nk_vec2 padding;
  3423. };
  3424. struct nk_style_combo {
  3425. /* background */
  3426. struct nk_style_item normal;
  3427. struct nk_style_item hover;
  3428. struct nk_style_item active;
  3429. struct nk_color border_color;
  3430. /* label */
  3431. struct nk_color label_normal;
  3432. struct nk_color label_hover;
  3433. struct nk_color label_active;
  3434. /* symbol */
  3435. struct nk_color symbol_normal;
  3436. struct nk_color symbol_hover;
  3437. struct nk_color symbol_active;
  3438. /* button */
  3439. struct nk_style_button button;
  3440. enum nk_symbol_type sym_normal;
  3441. enum nk_symbol_type sym_hover;
  3442. enum nk_symbol_type sym_active;
  3443. /* properties */
  3444. float border;
  3445. float rounding;
  3446. struct nk_vec2 content_padding;
  3447. struct nk_vec2 button_padding;
  3448. struct nk_vec2 spacing;
  3449. };
  3450. struct nk_style_tab {
  3451. /* background */
  3452. struct nk_style_item background;
  3453. struct nk_color border_color;
  3454. struct nk_color text;
  3455. /* button */
  3456. struct nk_style_button tab_maximize_button;
  3457. struct nk_style_button tab_minimize_button;
  3458. struct nk_style_button node_maximize_button;
  3459. struct nk_style_button node_minimize_button;
  3460. enum nk_symbol_type sym_minimize;
  3461. enum nk_symbol_type sym_maximize;
  3462. /* properties */
  3463. float border;
  3464. float rounding;
  3465. float indent;
  3466. struct nk_vec2 padding;
  3467. struct nk_vec2 spacing;
  3468. };
  3469. enum nk_style_header_align {
  3470. NK_HEADER_LEFT,
  3471. NK_HEADER_RIGHT
  3472. };
  3473. struct nk_style_window_header {
  3474. /* background */
  3475. struct nk_style_item normal;
  3476. struct nk_style_item hover;
  3477. struct nk_style_item active;
  3478. /* button */
  3479. struct nk_style_button close_button;
  3480. struct nk_style_button minimize_button;
  3481. enum nk_symbol_type close_symbol;
  3482. enum nk_symbol_type minimize_symbol;
  3483. enum nk_symbol_type maximize_symbol;
  3484. /* title */
  3485. struct nk_color label_normal;
  3486. struct nk_color label_hover;
  3487. struct nk_color label_active;
  3488. /* properties */
  3489. enum nk_style_header_align align;
  3490. struct nk_vec2 padding;
  3491. struct nk_vec2 label_padding;
  3492. struct nk_vec2 spacing;
  3493. };
  3494. struct nk_style_window {
  3495. struct nk_style_window_header header;
  3496. struct nk_style_item fixed_background;
  3497. struct nk_color background;
  3498. struct nk_color border_color;
  3499. struct nk_color popup_border_color;
  3500. struct nk_color combo_border_color;
  3501. struct nk_color contextual_border_color;
  3502. struct nk_color menu_border_color;
  3503. struct nk_color group_border_color;
  3504. struct nk_color tooltip_border_color;
  3505. struct nk_style_item scaler;
  3506. float border;
  3507. float combo_border;
  3508. float contextual_border;
  3509. float menu_border;
  3510. float group_border;
  3511. float tooltip_border;
  3512. float popup_border;
  3513. float min_row_height_padding;
  3514. float rounding;
  3515. struct nk_vec2 spacing;
  3516. struct nk_vec2 scrollbar_size;
  3517. struct nk_vec2 min_size;
  3518. struct nk_vec2 padding;
  3519. struct nk_vec2 group_padding;
  3520. struct nk_vec2 popup_padding;
  3521. struct nk_vec2 combo_padding;
  3522. struct nk_vec2 contextual_padding;
  3523. struct nk_vec2 menu_padding;
  3524. struct nk_vec2 tooltip_padding;
  3525. };
  3526. struct nk_style {
  3527. const struct nk_user_font *font;
  3528. const struct nk_cursor *cursors[NK_CURSOR_COUNT];
  3529. const struct nk_cursor *cursor_active;
  3530. struct nk_cursor *cursor_last;
  3531. int cursor_visible;
  3532. struct nk_style_text text;
  3533. struct nk_style_button button;
  3534. struct nk_style_button contextual_button;
  3535. struct nk_style_button menu_button;
  3536. struct nk_style_toggle option;
  3537. struct nk_style_toggle checkbox;
  3538. struct nk_style_selectable selectable;
  3539. struct nk_style_slider slider;
  3540. struct nk_style_progress progress;
  3541. struct nk_style_property property;
  3542. struct nk_style_edit edit;
  3543. struct nk_style_chart chart;
  3544. struct nk_style_scrollbar scrollh;
  3545. struct nk_style_scrollbar scrollv;
  3546. struct nk_style_tab tab;
  3547. struct nk_style_combo combo;
  3548. struct nk_style_window window;
  3549. };
  3550. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  3551. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  3552. NK_API struct nk_style_item nk_style_item_hide(void);
  3553. /*==============================================================
  3554. * PANEL
  3555. * =============================================================*/
  3556. #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
  3557. #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
  3558. #endif
  3559. #ifndef NK_CHART_MAX_SLOT
  3560. #define NK_CHART_MAX_SLOT 4
  3561. #endif
  3562. enum nk_panel_type {
  3563. NK_PANEL_WINDOW = NK_FLAG(0),
  3564. NK_PANEL_GROUP = NK_FLAG(1),
  3565. NK_PANEL_POPUP = NK_FLAG(2),
  3566. NK_PANEL_CONTEXTUAL = NK_FLAG(4),
  3567. NK_PANEL_COMBO = NK_FLAG(5),
  3568. NK_PANEL_MENU = NK_FLAG(6),
  3569. NK_PANEL_TOOLTIP = NK_FLAG(7)
  3570. };
  3571. enum nk_panel_set {
  3572. NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL|NK_PANEL_COMBO|NK_PANEL_MENU|NK_PANEL_TOOLTIP,
  3573. NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK|NK_PANEL_POPUP,
  3574. NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP|NK_PANEL_GROUP
  3575. };
  3576. struct nk_chart_slot {
  3577. enum nk_chart_type type;
  3578. struct nk_color color;
  3579. struct nk_color highlight;
  3580. float min, max, range;
  3581. int count;
  3582. struct nk_vec2 last;
  3583. int index;
  3584. };
  3585. struct nk_chart {
  3586. int slot;
  3587. float x, y, w, h;
  3588. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  3589. };
  3590. enum nk_panel_row_layout_type {
  3591. NK_LAYOUT_DYNAMIC_FIXED = 0,
  3592. NK_LAYOUT_DYNAMIC_ROW,
  3593. NK_LAYOUT_DYNAMIC_FREE,
  3594. NK_LAYOUT_DYNAMIC,
  3595. NK_LAYOUT_STATIC_FIXED,
  3596. NK_LAYOUT_STATIC_ROW,
  3597. NK_LAYOUT_STATIC_FREE,
  3598. NK_LAYOUT_STATIC,
  3599. NK_LAYOUT_TEMPLATE,
  3600. NK_LAYOUT_COUNT
  3601. };
  3602. struct nk_row_layout {
  3603. enum nk_panel_row_layout_type type;
  3604. int index;
  3605. float height;
  3606. float min_height;
  3607. int columns;
  3608. const float *ratio;
  3609. float item_width;
  3610. float item_height;
  3611. float item_offset;
  3612. float filled;
  3613. struct nk_rect item;
  3614. int tree_depth;
  3615. float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
  3616. };
  3617. struct nk_popup_buffer {
  3618. nk_size begin;
  3619. nk_size parent;
  3620. nk_size last;
  3621. nk_size end;
  3622. int active;
  3623. };
  3624. struct nk_menu_state {
  3625. float x, y, w, h;
  3626. struct nk_scroll offset;
  3627. };
  3628. struct nk_panel {
  3629. enum nk_panel_type type;
  3630. nk_flags flags;
  3631. struct nk_rect bounds;
  3632. nk_uint *offset_x;
  3633. nk_uint *offset_y;
  3634. float at_x, at_y, max_x;
  3635. float footer_height;
  3636. float header_height;
  3637. float border;
  3638. unsigned int has_scrolling;
  3639. struct nk_rect clip;
  3640. struct nk_menu_state menu;
  3641. struct nk_row_layout row;
  3642. struct nk_chart chart;
  3643. struct nk_command_buffer *buffer;
  3644. struct nk_panel *parent;
  3645. };
  3646. /*==============================================================
  3647. * WINDOW
  3648. * =============================================================*/
  3649. #ifndef NK_WINDOW_MAX_NAME
  3650. #define NK_WINDOW_MAX_NAME 64
  3651. #endif
  3652. struct nk_table;
  3653. enum nk_window_flags {
  3654. NK_WINDOW_PRIVATE = NK_FLAG(11),
  3655. NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE,
  3656. /* special window type growing up in height while being filled to a certain maximum height */
  3657. NK_WINDOW_ROM = NK_FLAG(12),
  3658. /* sets window widgets into a read only mode and does not allow input changes */
  3659. NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM|NK_WINDOW_NO_INPUT,
  3660. /* prevents all interaction caused by input to either window or widgets inside */
  3661. NK_WINDOW_HIDDEN = NK_FLAG(13),
  3662. /* Hides window and stops any window interaction and drawing */
  3663. NK_WINDOW_CLOSED = NK_FLAG(14),
  3664. /* Directly closes and frees the window at the end of the frame */
  3665. NK_WINDOW_MINIMIZED = NK_FLAG(15),
  3666. /* marks the window as minimized */
  3667. NK_WINDOW_REMOVE_ROM = NK_FLAG(16)
  3668. /* Removes read only mode at the end of the window */
  3669. };
  3670. struct nk_popup_state {
  3671. struct nk_window *win;
  3672. enum nk_panel_type type;
  3673. struct nk_popup_buffer buf;
  3674. nk_hash name;
  3675. int active;
  3676. unsigned combo_count;
  3677. unsigned con_count, con_old;
  3678. unsigned active_con;
  3679. struct nk_rect header;
  3680. };
  3681. struct nk_edit_state {
  3682. nk_hash name;
  3683. unsigned int seq;
  3684. unsigned int old;
  3685. int active, prev;
  3686. int cursor;
  3687. int sel_start;
  3688. int sel_end;
  3689. struct nk_scroll scrollbar;
  3690. unsigned char mode;
  3691. unsigned char single_line;
  3692. };
  3693. struct nk_property_state {
  3694. int active, prev;
  3695. char buffer[NK_MAX_NUMBER_BUFFER];
  3696. int length;
  3697. int cursor;
  3698. int select_start;
  3699. int select_end;
  3700. nk_hash name;
  3701. unsigned int seq;
  3702. unsigned int old;
  3703. int state;
  3704. };
  3705. struct nk_window {
  3706. unsigned int seq;
  3707. nk_hash name;
  3708. char name_string[NK_WINDOW_MAX_NAME];
  3709. nk_flags flags;
  3710. struct nk_rect bounds;
  3711. struct nk_scroll scrollbar;
  3712. struct nk_command_buffer buffer;
  3713. struct nk_panel *layout;
  3714. float scrollbar_hiding_timer;
  3715. /* persistent widget state */
  3716. struct nk_property_state property;
  3717. struct nk_popup_state popup;
  3718. struct nk_edit_state edit;
  3719. unsigned int scrolled;
  3720. struct nk_table *tables;
  3721. unsigned int table_count;
  3722. /* window list hooks */
  3723. struct nk_window *next;
  3724. struct nk_window *prev;
  3725. struct nk_window *parent;
  3726. };
  3727. /*==============================================================
  3728. * STACK
  3729. * =============================================================*/
  3730. /* The style modifier stack can be used to temporarily change a
  3731. * property inside `nk_style`. For example if you want a special
  3732. * red button you can temporarily push the old button color onto a stack
  3733. * draw the button with a red color and then you just pop the old color
  3734. * back from the stack:
  3735. *
  3736. * nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
  3737. * nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
  3738. * nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
  3739. * nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
  3740. *
  3741. * nk_button(...);
  3742. *
  3743. * nk_style_pop_style_item(ctx);
  3744. * nk_style_pop_style_item(ctx);
  3745. * nk_style_pop_style_item(ctx);
  3746. * nk_style_pop_vec2(ctx);
  3747. *
  3748. * Nuklear has a stack for style_items, float properties, vector properties,
  3749. * flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
  3750. * which can be changed at compile time.
  3751. */
  3752. #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
  3753. #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
  3754. #endif
  3755. #ifndef NK_FONT_STACK_SIZE
  3756. #define NK_FONT_STACK_SIZE 8
  3757. #endif
  3758. #ifndef NK_STYLE_ITEM_STACK_SIZE
  3759. #define NK_STYLE_ITEM_STACK_SIZE 16
  3760. #endif
  3761. #ifndef NK_FLOAT_STACK_SIZE
  3762. #define NK_FLOAT_STACK_SIZE 32
  3763. #endif
  3764. #ifndef NK_VECTOR_STACK_SIZE
  3765. #define NK_VECTOR_STACK_SIZE 16
  3766. #endif
  3767. #ifndef NK_FLAGS_STACK_SIZE
  3768. #define NK_FLAGS_STACK_SIZE 32
  3769. #endif
  3770. #ifndef NK_COLOR_STACK_SIZE
  3771. #define NK_COLOR_STACK_SIZE 32
  3772. #endif
  3773. #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
  3774. struct nk_config_stack_##name##_element {\
  3775. prefix##_##type *address;\
  3776. prefix##_##type old_value;\
  3777. }
  3778. #define NK_CONFIG_STACK(type,size)\
  3779. struct nk_config_stack_##type {\
  3780. int head;\
  3781. struct nk_config_stack_##type##_element elements[size];\
  3782. }
  3783. #define nk_float float
  3784. NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
  3785. NK_CONFIGURATION_STACK_TYPE(nk ,float, float);
  3786. NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
  3787. NK_CONFIGURATION_STACK_TYPE(nk ,flags, flags);
  3788. NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
  3789. NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
  3790. NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
  3791. NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
  3792. NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
  3793. NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
  3794. NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
  3795. NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
  3796. NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
  3797. NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
  3798. struct nk_configuration_stacks {
  3799. struct nk_config_stack_style_item style_items;
  3800. struct nk_config_stack_float floats;
  3801. struct nk_config_stack_vec2 vectors;
  3802. struct nk_config_stack_flags flags;
  3803. struct nk_config_stack_color colors;
  3804. struct nk_config_stack_user_font fonts;
  3805. struct nk_config_stack_button_behavior button_behaviors;
  3806. };
  3807. /*==============================================================
  3808. * CONTEXT
  3809. * =============================================================*/
  3810. #define NK_VALUE_PAGE_CAPACITY \
  3811. (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
  3812. struct nk_table {
  3813. unsigned int seq;
  3814. unsigned int size;
  3815. nk_hash keys[NK_VALUE_PAGE_CAPACITY];
  3816. nk_uint values[NK_VALUE_PAGE_CAPACITY];
  3817. struct nk_table *next, *prev;
  3818. };
  3819. union nk_page_data {
  3820. struct nk_table tbl;
  3821. struct nk_panel pan;
  3822. struct nk_window win;
  3823. };
  3824. struct nk_page_element {
  3825. union nk_page_data data;
  3826. struct nk_page_element *next;
  3827. struct nk_page_element *prev;
  3828. };
  3829. struct nk_page {
  3830. unsigned int size;
  3831. struct nk_page *next;
  3832. struct nk_page_element win[1];
  3833. };
  3834. struct nk_pool {
  3835. struct nk_allocator alloc;
  3836. enum nk_allocation_type type;
  3837. unsigned int page_count;
  3838. struct nk_page *pages;
  3839. struct nk_page_element *freelist;
  3840. unsigned capacity;
  3841. nk_size size;
  3842. nk_size cap;
  3843. };
  3844. struct nk_context {
  3845. /* public: can be accessed freely */
  3846. struct nk_input input;
  3847. struct nk_style style;
  3848. struct nk_buffer memory;
  3849. struct nk_clipboard clip;
  3850. nk_flags last_widget_state;
  3851. enum nk_button_behavior button_behavior;
  3852. struct nk_configuration_stacks stacks;
  3853. float delta_time_seconds;
  3854. /* private:
  3855. should only be accessed if you
  3856. know what you are doing */
  3857. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  3858. struct nk_draw_list draw_list;
  3859. #endif
  3860. #ifdef NK_INCLUDE_COMMAND_USERDATA
  3861. nk_handle userdata;
  3862. #endif
  3863. /* text editor objects are quite big because of an internal
  3864. * undo/redo stack. Therefore it does not make sense to have one for
  3865. * each window for temporary use cases, so I only provide *one* instance
  3866. * for all windows. This works because the content is cleared anyway */
  3867. struct nk_text_edit text_edit;
  3868. /* draw buffer used for overlay drawing operation like cursor */
  3869. struct nk_command_buffer overlay;
  3870. /* windows */
  3871. int build;
  3872. int use_pool;
  3873. struct nk_pool pool;
  3874. struct nk_window *begin;
  3875. struct nk_window *end;
  3876. struct nk_window *active;
  3877. struct nk_window *current;
  3878. struct nk_page_element *freelist;
  3879. unsigned int count;
  3880. unsigned int seq;
  3881. };
  3882. /* ==============================================================
  3883. * MATH
  3884. * =============================================================== */
  3885. #define NK_PI 3.141592654f
  3886. #define NK_UTF_INVALID 0xFFFD
  3887. #define NK_MAX_FLOAT_PRECISION 2
  3888. #define NK_UNUSED(x) ((void)(x))
  3889. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  3890. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  3891. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  3892. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
  3893. #define NK_INBOX(px, py, x, y, w, h)\
  3894. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  3895. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  3896. (!(((x1 > (x0 + w0)) || ((x1 + w1) < x0) || (y1 > (y0 + h0)) || (y1 + h1) < y0)))
  3897. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  3898. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  3899. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  3900. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  3901. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  3902. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  3903. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  3904. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  3905. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  3906. /* ==============================================================
  3907. * ALIGNMENT
  3908. * =============================================================== */
  3909. /* Pointer to Integer type conversion for pointer alignment */
  3910. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  3911. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  3912. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  3913. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  3914. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  3915. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  3916. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  3917. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  3918. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  3919. #else /* generates warning but works */
  3920. # define NK_UINT_TO_PTR(x) ((void*)(x))
  3921. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  3922. #endif
  3923. #define NK_ALIGN_PTR(x, mask)\
  3924. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  3925. #define NK_ALIGN_PTR_BACK(x, mask)\
  3926. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  3927. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  3928. #define NK_CONTAINER_OF(ptr,type,member)\
  3929. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  3930. #ifdef __cplusplus
  3931. }
  3932. #endif
  3933. #ifdef __cplusplus
  3934. template<typename T> struct nk_alignof;
  3935. template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
  3936. template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
  3937. template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
  3938. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
  3939. #define NK_ALIGNOF(t) (nk_alignof<t>::value)
  3940. #elif defined(_MSC_VER)
  3941. #define NK_ALIGNOF(t) (__alignof(t))
  3942. #else
  3943. #define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
  3944. #endif
  3945. #endif /* NK_H_ */
  3946. /*
  3947. * ==============================================================
  3948. *
  3949. * IMPLEMENTATION
  3950. *
  3951. * ===============================================================
  3952. */
  3953. #ifdef NK_IMPLEMENTATION
  3954. #ifndef NK_POOL_DEFAULT_CAPACITY
  3955. #define NK_POOL_DEFAULT_CAPACITY 16
  3956. #endif
  3957. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  3958. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  3959. #endif
  3960. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  3961. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  3962. #endif
  3963. /* standard library headers */
  3964. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3965. #include <stdlib.h> /* malloc, free */
  3966. #endif
  3967. #ifdef NK_INCLUDE_STANDARD_IO
  3968. #include <stdio.h> /* fopen, fclose,... */
  3969. #endif
  3970. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3971. #include <stdarg.h> /* valist, va_start, va_end, ... */
  3972. #endif
  3973. #ifndef NK_ASSERT
  3974. #include <assert.h>
  3975. #define NK_ASSERT(expr) assert(expr)
  3976. #endif
  3977. #ifndef NK_MEMSET
  3978. #define NK_MEMSET nk_memset
  3979. #endif
  3980. #ifndef NK_MEMCPY
  3981. #define NK_MEMCPY nk_memcopy
  3982. #endif
  3983. #ifndef NK_SQRT
  3984. #define NK_SQRT nk_sqrt
  3985. #endif
  3986. #ifndef NK_SIN
  3987. #define NK_SIN nk_sin
  3988. #endif
  3989. #ifndef NK_COS
  3990. #define NK_COS nk_cos
  3991. #endif
  3992. #ifndef NK_STRTOD
  3993. #define NK_STRTOD nk_strtod
  3994. #endif
  3995. #ifndef NK_DTOA
  3996. #define NK_DTOA nk_dtoa
  3997. #endif
  3998. #define NK_DEFAULT (-1)
  3999. #ifndef NK_VSNPRINTF
  4000. /* If your compiler does support `vsnprintf` I would highly recommend
  4001. * defining this to vsnprintf instead since `vsprintf` is basically
  4002. * unbelievable unsafe and should *NEVER* be used. But I have to support
  4003. * it since C89 only provides this unsafe version. */
  4004. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
  4005. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  4006. (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
  4007. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
  4008. defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
  4009. #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
  4010. #else
  4011. #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
  4012. #endif
  4013. #endif
  4014. #define NK_SCHAR_MIN (-127)
  4015. #define NK_SCHAR_MAX 127
  4016. #define NK_UCHAR_MIN 0
  4017. #define NK_UCHAR_MAX 256
  4018. #define NK_SSHORT_MIN (-32767)
  4019. #define NK_SSHORT_MAX 32767
  4020. #define NK_USHORT_MIN 0
  4021. #define NK_USHORT_MAX 65535
  4022. #define NK_SINT_MIN (-2147483647)
  4023. #define NK_SINT_MAX 2147483647
  4024. #define NK_UINT_MIN 0
  4025. #define NK_UINT_MAX 4294967295u
  4026. /* Make sure correct type size:
  4027. * This will fire with a negative subscript error if the type sizes
  4028. * are set incorrectly by the compiler, and compile out if not */
  4029. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  4030. NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
  4031. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  4032. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  4033. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  4034. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  4035. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  4036. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  4037. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  4038. NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
  4039. #define NK_FLOAT_PRECISION 0.00000000000001
  4040. NK_GLOBAL const struct nk_color nk_red = {255,0,0,255};
  4041. NK_GLOBAL const struct nk_color nk_green = {0,255,0,255};
  4042. NK_GLOBAL const struct nk_color nk_blue = {0,0,255,255};
  4043. NK_GLOBAL const struct nk_color nk_white = {255,255,255,255};
  4044. NK_GLOBAL const struct nk_color nk_black = {0,0,0,255};
  4045. NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
  4046. /*
  4047. * ==============================================================
  4048. *
  4049. * MATH
  4050. *
  4051. * ===============================================================
  4052. */
  4053. /* Since nuklear is supposed to work on all systems providing floating point
  4054. math without any dependencies I also had to implement my own math functions
  4055. for sqrt, sin and cos. Since the actual highly accurate implementations for
  4056. the standard library functions are quite complex and I do not need high
  4057. precision for my use cases I use approximations.
  4058. Sqrt
  4059. ----
  4060. For square root nuklear uses the famous fast inverse square root:
  4061. https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  4062. slightly tweaked magic constant. While on todays hardware it is
  4063. probably not faster it is still fast and accurate enough for
  4064. nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  4065. Sine/Cosine
  4066. -----------
  4067. All constants inside both function are generated Remez's minimax
  4068. approximations for value range 0...2*PI. The reason why I decided to
  4069. approximate exactly that range is that nuklear only needs sine and
  4070. cosine to generate circles which only requires that exact range.
  4071. In addition I used Remez instead of Taylor for additional precision:
  4072. www.lolengine.net/blog/2011/12/21/better-function-approximatations.
  4073. The tool I used to generate constants for both sine and cosine
  4074. (it can actually approximate a lot more functions) can be
  4075. found here: www.lolengine.net/wiki/oss/lolremez
  4076. */
  4077. NK_INTERN float
  4078. nk_inv_sqrt(float number)
  4079. {
  4080. float x2;
  4081. const float threehalfs = 1.5f;
  4082. union {nk_uint i; float f;} conv = {0};
  4083. conv.f = number;
  4084. x2 = number * 0.5f;
  4085. conv.i = 0x5f375A84 - (conv.i >> 1);
  4086. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  4087. return conv.f;
  4088. }
  4089. NK_INTERN float
  4090. nk_sqrt(float x)
  4091. {
  4092. return x * nk_inv_sqrt(x);
  4093. }
  4094. NK_INTERN float
  4095. nk_sin(float x)
  4096. {
  4097. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  4098. NK_STORAGE const float a1 = +1.00086760103908896f;
  4099. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  4100. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  4101. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  4102. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  4103. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  4104. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  4105. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  4106. }
  4107. NK_INTERN float
  4108. nk_cos(float x)
  4109. {
  4110. NK_STORAGE const float a0 = +1.00238601909309722f;
  4111. NK_STORAGE const float a1 = -3.81919947353040024e-2f;
  4112. NK_STORAGE const float a2 = -3.94382342128062756e-1f;
  4113. NK_STORAGE const float a3 = -1.18134036025221444e-1f;
  4114. NK_STORAGE const float a4 = +1.07123798512170878e-1f;
  4115. NK_STORAGE const float a5 = -1.86637164165180873e-2f;
  4116. NK_STORAGE const float a6 = +9.90140908664079833e-4f;
  4117. NK_STORAGE const float a7 = -5.23022132118824778e-14f;
  4118. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  4119. }
  4120. NK_INTERN nk_uint
  4121. nk_round_up_pow2(nk_uint v)
  4122. {
  4123. v--;
  4124. v |= v >> 1;
  4125. v |= v >> 2;
  4126. v |= v >> 4;
  4127. v |= v >> 8;
  4128. v |= v >> 16;
  4129. v++;
  4130. return v;
  4131. }
  4132. NK_API struct nk_rect
  4133. nk_get_null_rect(void)
  4134. {
  4135. return nk_null_rect;
  4136. }
  4137. NK_API struct nk_rect
  4138. nk_rect(float x, float y, float w, float h)
  4139. {
  4140. struct nk_rect r;
  4141. r.x = x; r.y = y;
  4142. r.w = w; r.h = h;
  4143. return r;
  4144. }
  4145. NK_API struct nk_rect
  4146. nk_recti(int x, int y, int w, int h)
  4147. {
  4148. struct nk_rect r;
  4149. r.x = (float)x;
  4150. r.y = (float)y;
  4151. r.w = (float)w;
  4152. r.h = (float)h;
  4153. return r;
  4154. }
  4155. NK_API struct nk_rect
  4156. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  4157. {
  4158. return nk_rect(pos.x, pos.y, size.x, size.y);
  4159. }
  4160. NK_API struct nk_rect
  4161. nk_rectv(const float *r)
  4162. {
  4163. return nk_rect(r[0], r[1], r[2], r[3]);
  4164. }
  4165. NK_API struct nk_rect
  4166. nk_rectiv(const int *r)
  4167. {
  4168. return nk_recti(r[0], r[1], r[2], r[3]);
  4169. }
  4170. NK_API struct nk_vec2
  4171. nk_rect_pos(struct nk_rect r)
  4172. {
  4173. struct nk_vec2 ret;
  4174. ret.x = r.x; ret.y = r.y;
  4175. return ret;
  4176. }
  4177. NK_API struct nk_vec2
  4178. nk_rect_size(struct nk_rect r)
  4179. {
  4180. struct nk_vec2 ret;
  4181. ret.x = r.w; ret.y = r.h;
  4182. return ret;
  4183. }
  4184. NK_INTERN struct nk_rect
  4185. nk_shrink_rect(struct nk_rect r, float amount)
  4186. {
  4187. struct nk_rect res;
  4188. r.w = NK_MAX(r.w, 2 * amount);
  4189. r.h = NK_MAX(r.h, 2 * amount);
  4190. res.x = r.x + amount;
  4191. res.y = r.y + amount;
  4192. res.w = r.w - 2 * amount;
  4193. res.h = r.h - 2 * amount;
  4194. return res;
  4195. }
  4196. NK_INTERN struct nk_rect
  4197. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  4198. {
  4199. r.w = NK_MAX(r.w, 2 * pad.x);
  4200. r.h = NK_MAX(r.h, 2 * pad.y);
  4201. r.x += pad.x; r.y += pad.y;
  4202. r.w -= 2 * pad.x;
  4203. r.h -= 2 * pad.y;
  4204. return r;
  4205. }
  4206. NK_API struct nk_vec2
  4207. nk_vec2(float x, float y)
  4208. {
  4209. struct nk_vec2 ret;
  4210. ret.x = x; ret.y = y;
  4211. return ret;
  4212. }
  4213. NK_API struct nk_vec2
  4214. nk_vec2i(int x, int y)
  4215. {
  4216. struct nk_vec2 ret;
  4217. ret.x = (float)x;
  4218. ret.y = (float)y;
  4219. return ret;
  4220. }
  4221. NK_API struct nk_vec2
  4222. nk_vec2v(const float *v)
  4223. {
  4224. return nk_vec2(v[0], v[1]);
  4225. }
  4226. NK_API struct nk_vec2
  4227. nk_vec2iv(const int *v)
  4228. {
  4229. return nk_vec2i(v[0], v[1]);
  4230. }
  4231. /*
  4232. * ==============================================================
  4233. *
  4234. * UTIL
  4235. *
  4236. * ===============================================================
  4237. */
  4238. NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
  4239. NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
  4240. NK_INTERN int nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
  4241. NK_INTERN int nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
  4242. NK_INTERN int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
  4243. NK_INTERN int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
  4244. NK_INTERN void*
  4245. nk_memcopy(void *dst0, const void *src0, nk_size length)
  4246. {
  4247. nk_ptr t;
  4248. char *dst = (char*)dst0;
  4249. const char *src = (const char*)src0;
  4250. if (length == 0 || dst == src)
  4251. goto done;
  4252. #define nk_word int
  4253. #define nk_wsize sizeof(nk_word)
  4254. #define nk_wmask (nk_wsize-1)
  4255. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  4256. #define NK_TLOOP1(s) do { s; } while (--t)
  4257. if (dst < src) {
  4258. t = (nk_ptr)src; /* only need low bits */
  4259. if ((t | (nk_ptr)dst) & nk_wmask) {
  4260. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  4261. t = length;
  4262. else
  4263. t = nk_wsize - (t & nk_wmask);
  4264. length -= t;
  4265. NK_TLOOP1(*dst++ = *src++);
  4266. }
  4267. t = length / nk_wsize;
  4268. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  4269. src += nk_wsize; dst += nk_wsize);
  4270. t = length & nk_wmask;
  4271. NK_TLOOP(*dst++ = *src++);
  4272. } else {
  4273. src += length;
  4274. dst += length;
  4275. t = (nk_ptr)src;
  4276. if ((t | (nk_ptr)dst) & nk_wmask) {
  4277. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  4278. t = length;
  4279. else
  4280. t &= nk_wmask;
  4281. length -= t;
  4282. NK_TLOOP1(*--dst = *--src);
  4283. }
  4284. t = length / nk_wsize;
  4285. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  4286. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  4287. t = length & nk_wmask;
  4288. NK_TLOOP(*--dst = *--src);
  4289. }
  4290. #undef nk_word
  4291. #undef nk_wsize
  4292. #undef nk_wmask
  4293. #undef NK_TLOOP
  4294. #undef NK_TLOOP1
  4295. done:
  4296. return (dst0);
  4297. }
  4298. NK_INTERN void
  4299. nk_memset(void *ptr, int c0, nk_size size)
  4300. {
  4301. #define nk_word unsigned
  4302. #define nk_wsize sizeof(nk_word)
  4303. #define nk_wmask (nk_wsize - 1)
  4304. nk_byte *dst = (nk_byte*)ptr;
  4305. unsigned c = 0;
  4306. nk_size t = 0;
  4307. if ((c = (nk_byte)c0) != 0) {
  4308. c = (c << 8) | c; /* at least 16-bits */
  4309. if (sizeof(unsigned int) > 2)
  4310. c = (c << 16) | c; /* at least 32-bits*/
  4311. }
  4312. /* too small of a word count */
  4313. dst = (nk_byte*)ptr;
  4314. if (size < 3 * nk_wsize) {
  4315. while (size--) *dst++ = (nk_byte)c0;
  4316. return;
  4317. }
  4318. /* align destination */
  4319. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  4320. t = nk_wsize -t;
  4321. size -= t;
  4322. do {
  4323. *dst++ = (nk_byte)c0;
  4324. } while (--t != 0);
  4325. }
  4326. /* fill word */
  4327. t = size / nk_wsize;
  4328. do {
  4329. *(nk_word*)((void*)dst) = c;
  4330. dst += nk_wsize;
  4331. } while (--t != 0);
  4332. /* fill trailing bytes */
  4333. t = (size & nk_wmask);
  4334. if (t != 0) {
  4335. do {
  4336. *dst++ = (nk_byte)c0;
  4337. } while (--t != 0);
  4338. }
  4339. #undef nk_word
  4340. #undef nk_wsize
  4341. #undef nk_wmask
  4342. }
  4343. NK_INTERN void
  4344. nk_zero(void *ptr, nk_size size)
  4345. {
  4346. NK_ASSERT(ptr);
  4347. NK_MEMSET(ptr, 0, size);
  4348. }
  4349. NK_API int
  4350. nk_strlen(const char *str)
  4351. {
  4352. int siz = 0;
  4353. NK_ASSERT(str);
  4354. while (str && *str++ != '\0') siz++;
  4355. return siz;
  4356. }
  4357. NK_API int
  4358. nk_strtoi(const char *str, const char **endptr)
  4359. {
  4360. int neg = 1;
  4361. const char *p = str;
  4362. int value = 0;
  4363. NK_ASSERT(str);
  4364. if (!str) return 0;
  4365. /* skip whitespace */
  4366. while (*p == ' ') p++;
  4367. if (*p == '-') {
  4368. neg = -1;
  4369. p++;
  4370. }
  4371. while (*p && *p >= '0' && *p <= '9') {
  4372. value = value * 10 + (int) (*p - '0');
  4373. p++;
  4374. }
  4375. if (endptr)
  4376. *endptr = p;
  4377. return neg*value;
  4378. }
  4379. NK_API double
  4380. nk_strtod(const char *str, const char **endptr)
  4381. {
  4382. double m;
  4383. double neg = 1.0;
  4384. const char *p = str;
  4385. double value = 0;
  4386. double number = 0;
  4387. NK_ASSERT(str);
  4388. if (!str) return 0;
  4389. /* skip whitespace */
  4390. while (*p == ' ') p++;
  4391. if (*p == '-') {
  4392. neg = -1.0;
  4393. p++;
  4394. }
  4395. while (*p && *p != '.' && *p != 'e') {
  4396. value = value * 10.0 + (double) (*p - '0');
  4397. p++;
  4398. }
  4399. if (*p == '.') {
  4400. p++;
  4401. for(m = 0.1; *p && *p != 'e'; p++ ) {
  4402. value = value + (double) (*p - '0') * m;
  4403. m *= 0.1;
  4404. }
  4405. }
  4406. if (*p == 'e') {
  4407. int i, pow, div;
  4408. p++;
  4409. if (*p == '-') {
  4410. div = nk_true;
  4411. p++;
  4412. } else if (*p == '+') {
  4413. div = nk_false;
  4414. p++;
  4415. } else div = nk_false;
  4416. for (pow = 0; *p; p++)
  4417. pow = pow * 10 + (int) (*p - '0');
  4418. for (m = 1.0, i = 0; i < pow; i++)
  4419. m *= 10.0;
  4420. if (div)
  4421. value /= m;
  4422. else value *= m;
  4423. }
  4424. number = value * neg;
  4425. if (endptr)
  4426. *endptr = p;
  4427. return number;
  4428. }
  4429. NK_API float
  4430. nk_strtof(const char *str, const char **endptr)
  4431. {
  4432. float float_value;
  4433. double double_value;
  4434. double_value = NK_STRTOD(str, endptr);
  4435. float_value = (float)double_value;
  4436. return float_value;
  4437. }
  4438. NK_API int
  4439. nk_stricmp(const char *s1, const char *s2)
  4440. {
  4441. nk_int c1,c2,d;
  4442. do {
  4443. c1 = *s1++;
  4444. c2 = *s2++;
  4445. d = c1 - c2;
  4446. while (d) {
  4447. if (c1 <= 'Z' && c1 >= 'A') {
  4448. d += ('a' - 'A');
  4449. if (!d) break;
  4450. }
  4451. if (c2 <= 'Z' && c2 >= 'A') {
  4452. d -= ('a' - 'A');
  4453. if (!d) break;
  4454. }
  4455. return ((d >= 0) << 1) - 1;
  4456. }
  4457. } while (c1);
  4458. return 0;
  4459. }
  4460. NK_API int
  4461. nk_stricmpn(const char *s1, const char *s2, int n)
  4462. {
  4463. int c1,c2,d;
  4464. NK_ASSERT(n >= 0);
  4465. do {
  4466. c1 = *s1++;
  4467. c2 = *s2++;
  4468. if (!n--) return 0;
  4469. d = c1 - c2;
  4470. while (d) {
  4471. if (c1 <= 'Z' && c1 >= 'A') {
  4472. d += ('a' - 'A');
  4473. if (!d) break;
  4474. }
  4475. if (c2 <= 'Z' && c2 >= 'A') {
  4476. d -= ('a' - 'A');
  4477. if (!d) break;
  4478. }
  4479. return ((d >= 0) << 1) - 1;
  4480. }
  4481. } while (c1);
  4482. return 0;
  4483. }
  4484. NK_INTERN int
  4485. nk_str_match_here(const char *regexp, const char *text)
  4486. {
  4487. if (regexp[0] == '\0')
  4488. return 1;
  4489. if (regexp[1] == '*')
  4490. return nk_str_match_star(regexp[0], regexp+2, text);
  4491. if (regexp[0] == '$' && regexp[1] == '\0')
  4492. return *text == '\0';
  4493. if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
  4494. return nk_str_match_here(regexp+1, text+1);
  4495. return 0;
  4496. }
  4497. NK_INTERN int
  4498. nk_str_match_star(int c, const char *regexp, const char *text)
  4499. {
  4500. do {/* a '* matches zero or more instances */
  4501. if (nk_str_match_here(regexp, text))
  4502. return 1;
  4503. } while (*text != '\0' && (*text++ == c || c == '.'));
  4504. return 0;
  4505. }
  4506. NK_API int
  4507. nk_strfilter(const char *text, const char *regexp)
  4508. {
  4509. /*
  4510. c matches any literal character c
  4511. . matches any single character
  4512. ^ matches the beginning of the input string
  4513. $ matches the end of the input string
  4514. * matches zero or more occurrences of the previous character*/
  4515. if (regexp[0] == '^')
  4516. return nk_str_match_here(regexp+1, text);
  4517. do { /* must look even if string is empty */
  4518. if (nk_str_match_here(regexp, text))
  4519. return 1;
  4520. } while (*text++ != '\0');
  4521. return 0;
  4522. }
  4523. NK_API int
  4524. nk_strmatch_fuzzy_text(const char *str, int str_len,
  4525. const char *pattern, int *out_score)
  4526. {
  4527. /* Returns true if each character in pattern is found sequentially within str
  4528. * if found then outScore is also set. Score value has no intrinsic meaning.
  4529. * Range varies with pattern. Can only compare scores with same search pattern. */
  4530. /* ------- scores --------- */
  4531. /* bonus for adjacent matches */
  4532. #define NK_ADJACENCY_BONUS 5
  4533. /* bonus if match occurs after a separator */
  4534. #define NK_SEPARATOR_BONUS 10
  4535. /* bonus if match is uppercase and prev is lower */
  4536. #define NK_CAMEL_BONUS 10
  4537. /* penalty applied for every letter in str before the first match */
  4538. #define NK_LEADING_LETTER_PENALTY (-3)
  4539. /* maximum penalty for leading letters */
  4540. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  4541. /* penalty for every letter that doesn't matter */
  4542. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  4543. /* loop variables */
  4544. int score = 0;
  4545. char const * pattern_iter = pattern;
  4546. int str_iter = 0;
  4547. int prev_matched = nk_false;
  4548. int prev_lower = nk_false;
  4549. /* true so if first letter match gets separator bonus*/
  4550. int prev_separator = nk_true;
  4551. /* use "best" matched letter if multiple string letters match the pattern */
  4552. char const * best_letter = 0;
  4553. int best_letter_score = 0;
  4554. /* loop over strings */
  4555. NK_ASSERT(str);
  4556. NK_ASSERT(pattern);
  4557. if (!str || !str_len || !pattern) return 0;
  4558. while (str_iter < str_len)
  4559. {
  4560. const char pattern_letter = *pattern_iter;
  4561. const char str_letter = str[str_iter];
  4562. int next_match = *pattern_iter != '\0' &&
  4563. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  4564. int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
  4565. int advanced = next_match && best_letter;
  4566. int pattern_repeat = best_letter && *pattern_iter != '\0';
  4567. pattern_repeat = pattern_repeat &&
  4568. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  4569. if (advanced || pattern_repeat) {
  4570. score += best_letter_score;
  4571. best_letter = 0;
  4572. best_letter_score = 0;
  4573. }
  4574. if (next_match || rematch)
  4575. {
  4576. int new_score = 0;
  4577. /* Apply penalty for each letter before the first pattern match */
  4578. if (pattern_iter == pattern) {
  4579. int count = (int)(&str[str_iter] - str);
  4580. int penalty = NK_LEADING_LETTER_PENALTY * count;
  4581. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  4582. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  4583. score += penalty;
  4584. }
  4585. /* apply bonus for consecutive bonuses */
  4586. if (prev_matched)
  4587. new_score += NK_ADJACENCY_BONUS;
  4588. /* apply bonus for matches after a separator */
  4589. if (prev_separator)
  4590. new_score += NK_SEPARATOR_BONUS;
  4591. /* apply bonus across camel case boundaries */
  4592. if (prev_lower && nk_is_upper(str_letter))
  4593. new_score += NK_CAMEL_BONUS;
  4594. /* update pattern iter IFF the next pattern letter was matched */
  4595. if (next_match)
  4596. ++pattern_iter;
  4597. /* update best letter in str which may be for a "next" letter or a rematch */
  4598. if (new_score >= best_letter_score) {
  4599. /* apply penalty for now skipped letter */
  4600. if (best_letter != 0)
  4601. score += NK_UNMATCHED_LETTER_PENALTY;
  4602. best_letter = &str[str_iter];
  4603. best_letter_score = new_score;
  4604. }
  4605. prev_matched = nk_true;
  4606. } else {
  4607. score += NK_UNMATCHED_LETTER_PENALTY;
  4608. prev_matched = nk_false;
  4609. }
  4610. /* separators should be more easily defined */
  4611. prev_lower = nk_is_lower(str_letter) != 0;
  4612. prev_separator = str_letter == '_' || str_letter == ' ';
  4613. ++str_iter;
  4614. }
  4615. /* apply score for last match */
  4616. if (best_letter)
  4617. score += best_letter_score;
  4618. /* did not match full pattern */
  4619. if (*pattern_iter != '\0')
  4620. return nk_false;
  4621. if (out_score)
  4622. *out_score = score;
  4623. return nk_true;
  4624. }
  4625. NK_API int
  4626. nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
  4627. {return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);}
  4628. NK_INTERN int
  4629. nk_string_float_limit(char *string, int prec)
  4630. {
  4631. int dot = 0;
  4632. char *c = string;
  4633. while (*c) {
  4634. if (*c == '.') {
  4635. dot = 1;
  4636. c++;
  4637. continue;
  4638. }
  4639. if (dot == (prec+1)) {
  4640. *c = 0;
  4641. break;
  4642. }
  4643. if (dot > 0) dot++;
  4644. c++;
  4645. }
  4646. return (int)(c - string);
  4647. }
  4648. NK_INTERN double
  4649. nk_pow(double x, int n)
  4650. {
  4651. /* check the sign of n */
  4652. double r = 1;
  4653. int plus = n >= 0;
  4654. n = (plus) ? n : -n;
  4655. while (n > 0) {
  4656. if ((n & 1) == 1)
  4657. r *= x;
  4658. n /= 2;
  4659. x *= x;
  4660. }
  4661. return plus ? r : 1.0 / r;
  4662. }
  4663. NK_INTERN int
  4664. nk_ifloord(double x)
  4665. {
  4666. x = (double)((int)x - ((x < 0.0) ? 1 : 0));
  4667. return (int)x;
  4668. }
  4669. NK_INTERN int
  4670. nk_ifloorf(float x)
  4671. {
  4672. x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
  4673. return (int)x;
  4674. }
  4675. NK_INTERN int
  4676. nk_iceilf(float x)
  4677. {
  4678. if (x >= 0) {
  4679. int i = (int)x;
  4680. return i;
  4681. } else {
  4682. int t = (int)x;
  4683. float r = x - (float)t;
  4684. return (r > 0.0f) ? t+1: t;
  4685. }
  4686. }
  4687. NK_INTERN int
  4688. nk_log10(double n)
  4689. {
  4690. int neg;
  4691. int ret;
  4692. int exp = 0;
  4693. neg = (n < 0) ? 1 : 0;
  4694. ret = (neg) ? (int)-n : (int)n;
  4695. while ((ret / 10) > 0) {
  4696. ret /= 10;
  4697. exp++;
  4698. }
  4699. if (neg) exp = -exp;
  4700. return exp;
  4701. }
  4702. NK_INTERN void
  4703. nk_strrev_ascii(char *s)
  4704. {
  4705. int len = nk_strlen(s);
  4706. int end = len / 2;
  4707. int i = 0;
  4708. char t;
  4709. for (; i < end; ++i) {
  4710. t = s[i];
  4711. s[i] = s[len - 1 - i];
  4712. s[len -1 - i] = t;
  4713. }
  4714. }
  4715. NK_INTERN char*
  4716. nk_itoa(char *s, long n)
  4717. {
  4718. long i = 0;
  4719. if (n == 0) {
  4720. s[i++] = '0';
  4721. s[i] = 0;
  4722. return s;
  4723. }
  4724. if (n < 0) {
  4725. s[i++] = '-';
  4726. n = -n;
  4727. }
  4728. while (n > 0) {
  4729. s[i++] = (char)('0' + (n % 10));
  4730. n /= 10;
  4731. }
  4732. s[i] = 0;
  4733. if (s[0] == '-')
  4734. ++s;
  4735. nk_strrev_ascii(s);
  4736. return s;
  4737. }
  4738. NK_INTERN char*
  4739. nk_dtoa(char *s, double n)
  4740. {
  4741. int useExp = 0;
  4742. int digit = 0, m = 0, m1 = 0;
  4743. char *c = s;
  4744. int neg = 0;
  4745. NK_ASSERT(s);
  4746. if (!s) return 0;
  4747. if (n == 0.0) {
  4748. s[0] = '0'; s[1] = '\0';
  4749. return s;
  4750. }
  4751. neg = (n < 0);
  4752. if (neg) n = -n;
  4753. /* calculate magnitude */
  4754. m = nk_log10(n);
  4755. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  4756. if (neg) *(c++) = '-';
  4757. /* set up for scientific notation */
  4758. if (useExp) {
  4759. if (m < 0)
  4760. m -= 1;
  4761. n = n / (double)nk_pow(10.0, m);
  4762. m1 = m;
  4763. m = 0;
  4764. }
  4765. if (m < 1.0) {
  4766. m = 0;
  4767. }
  4768. /* convert the number */
  4769. while (n > NK_FLOAT_PRECISION || m >= 0) {
  4770. double weight = nk_pow(10.0, m);
  4771. if (weight > 0) {
  4772. double t = (double)n / weight;
  4773. digit = nk_ifloord(t);
  4774. n -= ((double)digit * weight);
  4775. *(c++) = (char)('0' + (char)digit);
  4776. }
  4777. if (m == 0 && n > 0)
  4778. *(c++) = '.';
  4779. m--;
  4780. }
  4781. if (useExp) {
  4782. /* convert the exponent */
  4783. int i, j;
  4784. *(c++) = 'e';
  4785. if (m1 > 0) {
  4786. *(c++) = '+';
  4787. } else {
  4788. *(c++) = '-';
  4789. m1 = -m1;
  4790. }
  4791. m = 0;
  4792. while (m1 > 0) {
  4793. *(c++) = (char)('0' + (char)(m1 % 10));
  4794. m1 /= 10;
  4795. m++;
  4796. }
  4797. c -= m;
  4798. for (i = 0, j = m-1; i<j; i++, j--) {
  4799. /* swap without temporary */
  4800. c[i] ^= c[j];
  4801. c[j] ^= c[i];
  4802. c[i] ^= c[j];
  4803. }
  4804. c += m;
  4805. }
  4806. *(c) = '\0';
  4807. return s;
  4808. }
  4809. #ifdef NK_INCLUDE_STANDARD_VARARGS
  4810. #ifndef NK_INCLUDE_STANDARD_IO
  4811. static int
  4812. nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
  4813. {
  4814. enum nk_arg_type {
  4815. NK_ARG_TYPE_CHAR,
  4816. NK_ARG_TYPE_SHORT,
  4817. NK_ARG_TYPE_DEFAULT,
  4818. NK_ARG_TYPE_LONG
  4819. };
  4820. enum nk_arg_flags {
  4821. NK_ARG_FLAG_LEFT = 0x01,
  4822. NK_ARG_FLAG_PLUS = 0x02,
  4823. NK_ARG_FLAG_SPACE = 0x04,
  4824. NK_ARG_FLAG_NUM = 0x10,
  4825. NK_ARG_FLAG_ZERO = 0x20
  4826. };
  4827. char number_buffer[NK_MAX_NUMBER_BUFFER];
  4828. enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
  4829. int precision = NK_DEFAULT;
  4830. int width = NK_DEFAULT;
  4831. nk_flags flag = 0;
  4832. int len = 0;
  4833. int result = -1;
  4834. const char *iter = fmt;
  4835. NK_ASSERT(buf);
  4836. NK_ASSERT(buf_size);
  4837. if (!buf || !buf_size || !fmt) return 0;
  4838. for (iter = fmt; *iter && len < buf_size; iter++) {
  4839. /* copy all non-format characters */
  4840. while (*iter && (*iter != '%') && (len < buf_size))
  4841. buf[len++] = *iter++;
  4842. if (!(*iter) || len >= buf_size) break;
  4843. iter++;
  4844. /* flag arguments */
  4845. while (*iter) {
  4846. if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
  4847. else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
  4848. else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
  4849. else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
  4850. else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
  4851. else break;
  4852. iter++;
  4853. }
  4854. /* width argument */
  4855. width = NK_DEFAULT;
  4856. if (*iter >= '1' && *iter <= '9') {
  4857. const char *end;
  4858. width = nk_strtoi(iter, &end);
  4859. if (end == iter)
  4860. width = -1;
  4861. else iter = end;
  4862. } else if (*iter == '*') {
  4863. width = va_arg(args, int);
  4864. iter++;
  4865. }
  4866. /* precision argument */
  4867. precision = NK_DEFAULT;
  4868. if (*iter == '.') {
  4869. iter++;
  4870. if (*iter == '*') {
  4871. precision = va_arg(args, int);
  4872. iter++;
  4873. } else {
  4874. const char *end;
  4875. precision = nk_strtoi(iter, &end);
  4876. if (end == iter)
  4877. precision = -1;
  4878. else iter = end;
  4879. }
  4880. }
  4881. /* length modifier */
  4882. if (*iter == 'h') {
  4883. if (*(iter+1) == 'h') {
  4884. arg_type = NK_ARG_TYPE_CHAR;
  4885. iter++;
  4886. } else arg_type = NK_ARG_TYPE_SHORT;
  4887. iter++;
  4888. } else if (*iter == 'l') {
  4889. arg_type = NK_ARG_TYPE_LONG;
  4890. iter++;
  4891. } else arg_type = NK_ARG_TYPE_DEFAULT;
  4892. /* specifier */
  4893. if (*iter == '%') {
  4894. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  4895. NK_ASSERT(precision == NK_DEFAULT);
  4896. NK_ASSERT(width == NK_DEFAULT);
  4897. if (len < buf_size)
  4898. buf[len++] = '%';
  4899. } else if (*iter == 's') {
  4900. /* string */
  4901. const char *str = va_arg(args, const char*);
  4902. NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
  4903. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  4904. NK_ASSERT(precision == NK_DEFAULT);
  4905. NK_ASSERT(width == NK_DEFAULT);
  4906. if (str == buf) return -1;
  4907. while (str && *str && len < buf_size)
  4908. buf[len++] = *str++;
  4909. } else if (*iter == 'n') {
  4910. /* current length callback */
  4911. signed int *n = va_arg(args, int*);
  4912. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  4913. NK_ASSERT(precision == NK_DEFAULT);
  4914. NK_ASSERT(width == NK_DEFAULT);
  4915. if (n) *n = len;
  4916. } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
  4917. /* signed integer */
  4918. long value = 0;
  4919. const char *num_iter;
  4920. int num_len, num_print, padding;
  4921. int cur_precision = NK_MAX(precision, 1);
  4922. int cur_width = NK_MAX(width, 0);
  4923. /* retrieve correct value type */
  4924. if (arg_type == NK_ARG_TYPE_CHAR)
  4925. value = (signed char)va_arg(args, int);
  4926. else if (arg_type == NK_ARG_TYPE_SHORT)
  4927. value = (signed short)va_arg(args, int);
  4928. else if (arg_type == NK_ARG_TYPE_LONG)
  4929. value = va_arg(args, signed long);
  4930. else if (*iter == 'c')
  4931. value = (unsigned char)va_arg(args, int);
  4932. else value = va_arg(args, signed int);
  4933. /* convert number to string */
  4934. nk_itoa(number_buffer, value);
  4935. num_len = nk_strlen(number_buffer);
  4936. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  4937. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  4938. padding = NK_MAX(padding-1, 0);
  4939. /* fill left padding up to a total of `width` characters */
  4940. if (!(flag & NK_ARG_FLAG_LEFT)) {
  4941. while (padding-- > 0 && (len < buf_size)) {
  4942. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  4943. buf[len++] = '0';
  4944. else buf[len++] = ' ';
  4945. }
  4946. }
  4947. /* copy string value representation into buffer */
  4948. if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
  4949. buf[len++] = '+';
  4950. else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
  4951. buf[len++] = ' ';
  4952. /* fill up to precision number of digits with '0' */
  4953. num_print = NK_MAX(cur_precision, num_len);
  4954. while (precision && (num_print > num_len) && (len < buf_size)) {
  4955. buf[len++] = '0';
  4956. num_print--;
  4957. }
  4958. /* copy string value representation into buffer */
  4959. num_iter = number_buffer;
  4960. while (precision && *num_iter && len < buf_size)
  4961. buf[len++] = *num_iter++;
  4962. /* fill right padding up to width characters */
  4963. if (flag & NK_ARG_FLAG_LEFT) {
  4964. while ((padding-- > 0) && (len < buf_size))
  4965. buf[len++] = ' ';
  4966. }
  4967. } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
  4968. /* unsigned integer */
  4969. unsigned long value = 0;
  4970. int num_len = 0, num_print, padding = 0;
  4971. int cur_precision = NK_MAX(precision, 1);
  4972. int cur_width = NK_MAX(width, 0);
  4973. unsigned int base = (*iter == 'o') ? 8: (*iter == 'u')? 10: 16;
  4974. /* print oct/hex/dec value */
  4975. const char *upper_output_format = "0123456789ABCDEF";
  4976. const char *lower_output_format = "0123456789abcdef";
  4977. const char *output_format = (*iter == 'x') ?
  4978. lower_output_format: upper_output_format;
  4979. /* retrieve correct value type */
  4980. if (arg_type == NK_ARG_TYPE_CHAR)
  4981. value = (unsigned char)va_arg(args, int);
  4982. else if (arg_type == NK_ARG_TYPE_SHORT)
  4983. value = (unsigned short)va_arg(args, int);
  4984. else if (arg_type == NK_ARG_TYPE_LONG)
  4985. value = va_arg(args, unsigned long);
  4986. else value = va_arg(args, unsigned int);
  4987. do {
  4988. /* convert decimal number into hex/oct number */
  4989. int digit = output_format[value % base];
  4990. if (num_len < NK_MAX_NUMBER_BUFFER)
  4991. number_buffer[num_len++] = (char)digit;
  4992. value /= base;
  4993. } while (value > 0);
  4994. num_print = NK_MAX(cur_precision, num_len);
  4995. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  4996. if (flag & NK_ARG_FLAG_NUM)
  4997. padding = NK_MAX(padding-1, 0);
  4998. /* fill left padding up to a total of `width` characters */
  4999. if (!(flag & NK_ARG_FLAG_LEFT)) {
  5000. while ((padding-- > 0) && (len < buf_size)) {
  5001. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  5002. buf[len++] = '0';
  5003. else buf[len++] = ' ';
  5004. }
  5005. }
  5006. /* fill up to precision number of digits */
  5007. if (num_print && (flag & NK_ARG_FLAG_NUM)) {
  5008. if ((*iter == 'o') && (len < buf_size)) {
  5009. buf[len++] = '0';
  5010. } else if ((*iter == 'x') && ((len+1) < buf_size)) {
  5011. buf[len++] = '0';
  5012. buf[len++] = 'x';
  5013. } else if ((*iter == 'X') && ((len+1) < buf_size)) {
  5014. buf[len++] = '0';
  5015. buf[len++] = 'X';
  5016. }
  5017. }
  5018. while (precision && (num_print > num_len) && (len < buf_size)) {
  5019. buf[len++] = '0';
  5020. num_print--;
  5021. }
  5022. /* reverse number direction */
  5023. while (num_len > 0) {
  5024. if (precision && (len < buf_size))
  5025. buf[len++] = number_buffer[num_len-1];
  5026. num_len--;
  5027. }
  5028. /* fill right padding up to width characters */
  5029. if (flag & NK_ARG_FLAG_LEFT) {
  5030. while ((padding-- > 0) && (len < buf_size))
  5031. buf[len++] = ' ';
  5032. }
  5033. } else if (*iter == 'f') {
  5034. /* floating point */
  5035. const char *num_iter;
  5036. int cur_precision = (precision < 0) ? 6: precision;
  5037. int prefix, cur_width = NK_MAX(width, 0);
  5038. double value = va_arg(args, double);
  5039. int num_len = 0, frac_len = 0, dot = 0;
  5040. int padding = 0;
  5041. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  5042. NK_DTOA(number_buffer, value);
  5043. num_len = nk_strlen(number_buffer);
  5044. /* calculate padding */
  5045. num_iter = number_buffer;
  5046. while (*num_iter && *num_iter != '.')
  5047. num_iter++;
  5048. prefix = (*num_iter == '.')?(int)(num_iter - number_buffer)+1:0;
  5049. padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
  5050. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  5051. padding = NK_MAX(padding-1, 0);
  5052. /* fill left padding up to a total of `width` characters */
  5053. if (!(flag & NK_ARG_FLAG_LEFT)) {
  5054. while (padding-- > 0 && (len < buf_size)) {
  5055. if (flag & NK_ARG_FLAG_ZERO)
  5056. buf[len++] = '0';
  5057. else buf[len++] = ' ';
  5058. }
  5059. }
  5060. /* copy string value representation into buffer */
  5061. num_iter = number_buffer;
  5062. if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
  5063. buf[len++] = '+';
  5064. else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
  5065. buf[len++] = ' ';
  5066. while (*num_iter) {
  5067. if (dot) frac_len++;
  5068. if (len < buf_size)
  5069. buf[len++] = *num_iter;
  5070. if (*num_iter == '.') dot = 1;
  5071. if (frac_len >= cur_precision) break;
  5072. num_iter++;
  5073. }
  5074. /* fill number up to precision */
  5075. while (frac_len < cur_precision) {
  5076. if (!dot && len < buf_size) {
  5077. buf[len++] = '.';
  5078. dot = 1;
  5079. }
  5080. if (len < buf_size)
  5081. buf[len++] = '0';
  5082. frac_len++;
  5083. }
  5084. /* fill right padding up to width characters */
  5085. if (flag & NK_ARG_FLAG_LEFT) {
  5086. while ((padding-- > 0) && (len < buf_size))
  5087. buf[len++] = ' ';
  5088. }
  5089. } else {
  5090. /* Specifier not supported: g,G,e,E,p,z */
  5091. NK_ASSERT(0 && "specifier is not supported!");
  5092. return result;
  5093. }
  5094. }
  5095. buf[(len >= buf_size)?(buf_size-1):len] = 0;
  5096. result = (len >= buf_size)?-1:len;
  5097. return result;
  5098. }
  5099. #endif
  5100. NK_INTERN int
  5101. nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
  5102. {
  5103. int result = -1;
  5104. NK_ASSERT(buf);
  5105. NK_ASSERT(buf_size);
  5106. if (!buf || !buf_size || !fmt) return 0;
  5107. #ifdef NK_INCLUDE_STANDARD_IO
  5108. result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
  5109. result = (result >= buf_size) ? -1: result;
  5110. buf[buf_size-1] = 0;
  5111. #else
  5112. result = nk_vsnprintf(buf, buf_size, fmt, args);
  5113. #endif
  5114. return result;
  5115. }
  5116. #endif
  5117. NK_API nk_hash
  5118. nk_murmur_hash(const void * key, int len, nk_hash seed)
  5119. {
  5120. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  5121. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  5122. union {const nk_uint *i; const nk_byte *b;} conv = {0};
  5123. const nk_byte *data = (const nk_byte*)key;
  5124. const int nblocks = len/4;
  5125. nk_uint h1 = seed;
  5126. const nk_uint c1 = 0xcc9e2d51;
  5127. const nk_uint c2 = 0x1b873593;
  5128. const nk_byte *tail;
  5129. const nk_uint *blocks;
  5130. nk_uint k1;
  5131. int i;
  5132. /* body */
  5133. if (!key) return 0;
  5134. conv.b = (data + nblocks*4);
  5135. blocks = (const nk_uint*)conv.i;
  5136. for (i = -nblocks; i; ++i) {
  5137. k1 = blocks[i];
  5138. k1 *= c1;
  5139. k1 = NK_ROTL(k1,15);
  5140. k1 *= c2;
  5141. h1 ^= k1;
  5142. h1 = NK_ROTL(h1,13);
  5143. h1 = h1*5+0xe6546b64;
  5144. }
  5145. /* tail */
  5146. tail = (const nk_byte*)(data + nblocks*4);
  5147. k1 = 0;
  5148. switch (len & 3) {
  5149. case 3: k1 ^= (nk_uint)(tail[2] << 16);
  5150. case 2: k1 ^= (nk_uint)(tail[1] << 8u);
  5151. case 1: k1 ^= tail[0];
  5152. k1 *= c1;
  5153. k1 = NK_ROTL(k1,15);
  5154. k1 *= c2;
  5155. h1 ^= k1;
  5156. default: break;
  5157. }
  5158. /* finalization */
  5159. h1 ^= (nk_uint)len;
  5160. /* fmix32 */
  5161. h1 ^= h1 >> 16;
  5162. h1 *= 0x85ebca6b;
  5163. h1 ^= h1 >> 13;
  5164. h1 *= 0xc2b2ae35;
  5165. h1 ^= h1 >> 16;
  5166. #undef NK_ROTL
  5167. return h1;
  5168. }
  5169. #ifdef NK_INCLUDE_STANDARD_IO
  5170. NK_INTERN char*
  5171. nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc)
  5172. {
  5173. char *buf;
  5174. FILE *fd;
  5175. long ret;
  5176. NK_ASSERT(path);
  5177. NK_ASSERT(siz);
  5178. NK_ASSERT(alloc);
  5179. if (!path || !siz || !alloc)
  5180. return 0;
  5181. fd = fopen(path, "rb");
  5182. if (!fd) return 0;
  5183. fseek(fd, 0, SEEK_END);
  5184. ret = ftell(fd);
  5185. if (ret < 0) {
  5186. fclose(fd);
  5187. return 0;
  5188. }
  5189. *siz = (nk_size)ret;
  5190. fseek(fd, 0, SEEK_SET);
  5191. buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
  5192. NK_ASSERT(buf);
  5193. if (!buf) {
  5194. fclose(fd);
  5195. return 0;
  5196. }
  5197. *siz = (nk_size)fread(buf, *siz, 1, fd);
  5198. fclose(fd);
  5199. return buf;
  5200. }
  5201. #endif
  5202. /*
  5203. * ==============================================================
  5204. *
  5205. * COLOR
  5206. *
  5207. * ===============================================================
  5208. */
  5209. NK_INTERN int
  5210. nk_parse_hex(const char *p, int length)
  5211. {
  5212. int i = 0;
  5213. int len = 0;
  5214. while (len < length) {
  5215. i <<= 4;
  5216. if (p[len] >= 'a' && p[len] <= 'f')
  5217. i += ((p[len] - 'a') + 10);
  5218. else if (p[len] >= 'A' && p[len] <= 'F')
  5219. i += ((p[len] - 'A') + 10);
  5220. else i += (p[len] - '0');
  5221. len++;
  5222. }
  5223. return i;
  5224. }
  5225. NK_API struct nk_color
  5226. nk_rgba(int r, int g, int b, int a)
  5227. {
  5228. struct nk_color ret;
  5229. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  5230. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  5231. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  5232. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  5233. return ret;
  5234. }
  5235. NK_API struct nk_color
  5236. nk_rgb_hex(const char *rgb)
  5237. {
  5238. struct nk_color col;
  5239. const char *c = rgb;
  5240. if (*c == '#') c++;
  5241. col.r = (nk_byte)nk_parse_hex(c, 2);
  5242. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  5243. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  5244. col.a = 255;
  5245. return col;
  5246. }
  5247. NK_API struct nk_color
  5248. nk_rgba_hex(const char *rgb)
  5249. {
  5250. struct nk_color col;
  5251. const char *c = rgb;
  5252. if (*c == '#') c++;
  5253. col.r = (nk_byte)nk_parse_hex(c, 2);
  5254. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  5255. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  5256. col.a = (nk_byte)nk_parse_hex(c+6, 2);
  5257. return col;
  5258. }
  5259. NK_API void
  5260. nk_color_hex_rgba(char *output, struct nk_color col)
  5261. {
  5262. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  5263. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  5264. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  5265. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  5266. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  5267. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  5268. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  5269. output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  5270. output[7] = (char)NK_TO_HEX((col.a & 0x0F));
  5271. output[8] = '\0';
  5272. #undef NK_TO_HEX
  5273. }
  5274. NK_API void
  5275. nk_color_hex_rgb(char *output, struct nk_color col)
  5276. {
  5277. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  5278. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  5279. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  5280. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  5281. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  5282. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  5283. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  5284. output[6] = '\0';
  5285. #undef NK_TO_HEX
  5286. }
  5287. NK_API struct nk_color
  5288. nk_rgba_iv(const int *c)
  5289. {
  5290. return nk_rgba(c[0], c[1], c[2], c[3]);
  5291. }
  5292. NK_API struct nk_color
  5293. nk_rgba_bv(const nk_byte *c)
  5294. {
  5295. return nk_rgba(c[0], c[1], c[2], c[3]);
  5296. }
  5297. NK_API struct nk_color
  5298. nk_rgb(int r, int g, int b)
  5299. {
  5300. struct nk_color ret;
  5301. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  5302. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  5303. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  5304. ret.a = (nk_byte)255;
  5305. return ret;
  5306. }
  5307. NK_API struct nk_color
  5308. nk_rgb_iv(const int *c)
  5309. {
  5310. return nk_rgb(c[0], c[1], c[2]);
  5311. }
  5312. NK_API struct nk_color
  5313. nk_rgb_bv(const nk_byte* c)
  5314. {
  5315. return nk_rgb(c[0], c[1], c[2]);
  5316. }
  5317. NK_API struct nk_color
  5318. nk_rgba_u32(nk_uint in)
  5319. {
  5320. struct nk_color ret;
  5321. ret.r = (in & 0xFF);
  5322. ret.g = ((in >> 8) & 0xFF);
  5323. ret.b = ((in >> 16) & 0xFF);
  5324. ret.a = (nk_byte)((in >> 24) & 0xFF);
  5325. return ret;
  5326. }
  5327. NK_API struct nk_color
  5328. nk_rgba_f(float r, float g, float b, float a)
  5329. {
  5330. struct nk_color ret;
  5331. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  5332. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  5333. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  5334. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  5335. return ret;
  5336. }
  5337. NK_API struct nk_color
  5338. nk_rgba_fv(const float *c)
  5339. {
  5340. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  5341. }
  5342. NK_API struct nk_color
  5343. nk_rgb_f(float r, float g, float b)
  5344. {
  5345. struct nk_color ret;
  5346. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  5347. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  5348. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  5349. ret.a = 255;
  5350. return ret;
  5351. }
  5352. NK_API struct nk_color
  5353. nk_rgb_fv(const float *c)
  5354. {
  5355. return nk_rgb_f(c[0], c[1], c[2]);
  5356. }
  5357. NK_API struct nk_color
  5358. nk_hsv(int h, int s, int v)
  5359. {
  5360. return nk_hsva(h, s, v, 255);
  5361. }
  5362. NK_API struct nk_color
  5363. nk_hsv_iv(const int *c)
  5364. {
  5365. return nk_hsv(c[0], c[1], c[2]);
  5366. }
  5367. NK_API struct nk_color
  5368. nk_hsv_bv(const nk_byte *c)
  5369. {
  5370. return nk_hsv(c[0], c[1], c[2]);
  5371. }
  5372. NK_API struct nk_color
  5373. nk_hsv_f(float h, float s, float v)
  5374. {
  5375. return nk_hsva_f(h, s, v, 1.0f);
  5376. }
  5377. NK_API struct nk_color
  5378. nk_hsv_fv(const float *c)
  5379. {
  5380. return nk_hsv_f(c[0], c[1], c[2]);
  5381. }
  5382. NK_API struct nk_color
  5383. nk_hsva(int h, int s, int v, int a)
  5384. {
  5385. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  5386. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  5387. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  5388. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  5389. return nk_hsva_f(hf, sf, vf, af);
  5390. }
  5391. NK_API struct nk_color
  5392. nk_hsva_iv(const int *c)
  5393. {
  5394. return nk_hsva(c[0], c[1], c[2], c[3]);
  5395. }
  5396. NK_API struct nk_color
  5397. nk_hsva_bv(const nk_byte *c)
  5398. {
  5399. return nk_hsva(c[0], c[1], c[2], c[3]);
  5400. }
  5401. NK_API struct nk_color
  5402. nk_hsva_f(float h, float s, float v, float a)
  5403. {
  5404. struct nk_colorf out = {0,0,0,0};
  5405. float p, q, t, f;
  5406. int i;
  5407. if (s <= 0.0f) {
  5408. out.r = v; out.g = v; out.b = v;
  5409. return nk_rgb_f(out.r, out.g, out.b);
  5410. }
  5411. h = h / (60.0f/360.0f);
  5412. i = (int)h;
  5413. f = h - (float)i;
  5414. p = v * (1.0f - s);
  5415. q = v * (1.0f - (s * f));
  5416. t = v * (1.0f - s * (1.0f - f));
  5417. switch (i) {
  5418. case 0: default: out.r = v; out.g = t; out.b = p; break;
  5419. case 1: out.r = q; out.g = v; out.b = p; break;
  5420. case 2: out.r = p; out.g = v; out.b = t; break;
  5421. case 3: out.r = p; out.g = q; out.b = v; break;
  5422. case 4: out.r = t; out.g = p; out.b = v; break;
  5423. case 5: out.r = v; out.g = p; out.b = q; break;
  5424. }
  5425. return nk_rgba_f(out.r, out.g, out.b, a);
  5426. }
  5427. NK_API struct nk_color
  5428. nk_hsva_fv(const float *c)
  5429. {
  5430. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  5431. }
  5432. NK_API nk_uint
  5433. nk_color_u32(struct nk_color in)
  5434. {
  5435. nk_uint out = (nk_uint)in.r;
  5436. out |= ((nk_uint)in.g << 8);
  5437. out |= ((nk_uint)in.b << 16);
  5438. out |= ((nk_uint)in.a << 24);
  5439. return out;
  5440. }
  5441. NK_API void
  5442. nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
  5443. {
  5444. NK_STORAGE const float s = 1.0f/255.0f;
  5445. *r = (float)in.r * s;
  5446. *g = (float)in.g * s;
  5447. *b = (float)in.b * s;
  5448. *a = (float)in.a * s;
  5449. }
  5450. NK_API void
  5451. nk_color_fv(float *c, struct nk_color in)
  5452. {
  5453. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  5454. }
  5455. NK_API void
  5456. nk_color_d(double *r, double *g, double *b, double *a, struct nk_color in)
  5457. {
  5458. NK_STORAGE const double s = 1.0/255.0;
  5459. *r = (double)in.r * s;
  5460. *g = (double)in.g * s;
  5461. *b = (double)in.b * s;
  5462. *a = (double)in.a * s;
  5463. }
  5464. NK_API void
  5465. nk_color_dv(double *c, struct nk_color in)
  5466. {
  5467. nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
  5468. }
  5469. NK_API void
  5470. nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
  5471. {
  5472. float a;
  5473. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  5474. }
  5475. NK_API void
  5476. nk_color_hsv_fv(float *out, struct nk_color in)
  5477. {
  5478. float a;
  5479. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  5480. }
  5481. NK_API void
  5482. nk_color_hsva_f(float *out_h, float *out_s,
  5483. float *out_v, float *out_a, struct nk_color in)
  5484. {
  5485. float chroma;
  5486. float K = 0.0f;
  5487. float r,g,b,a;
  5488. nk_color_f(&r,&g,&b,&a, in);
  5489. if (g < b) {
  5490. const float t = g; g = b; b = t;
  5491. K = -1.f;
  5492. }
  5493. if (r < g) {
  5494. const float t = r; r = g; g = t;
  5495. K = -2.f/6.0f - K;
  5496. }
  5497. chroma = r - ((g < b) ? g: b);
  5498. *out_h = NK_ABS(K + (g - b)/(6.0f * chroma + 1e-20f));
  5499. *out_s = chroma / (r + 1e-20f);
  5500. *out_v = r;
  5501. *out_a = (float)in.a / 255.0f;
  5502. }
  5503. NK_API void
  5504. nk_color_hsva_fv(float *out, struct nk_color in)
  5505. {
  5506. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  5507. }
  5508. NK_API void
  5509. nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
  5510. int *out_a, struct nk_color in)
  5511. {
  5512. float h,s,v,a;
  5513. nk_color_hsva_f(&h, &s, &v, &a, in);
  5514. *out_h = (nk_byte)(h * 255.0f);
  5515. *out_s = (nk_byte)(s * 255.0f);
  5516. *out_v = (nk_byte)(v * 255.0f);
  5517. *out_a = (nk_byte)(a * 255.0f);
  5518. }
  5519. NK_API void
  5520. nk_color_hsva_iv(int *out, struct nk_color in)
  5521. {
  5522. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  5523. }
  5524. NK_API void
  5525. nk_color_hsva_bv(nk_byte *out, struct nk_color in)
  5526. {
  5527. int tmp[4];
  5528. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  5529. out[0] = (nk_byte)tmp[0];
  5530. out[1] = (nk_byte)tmp[1];
  5531. out[2] = (nk_byte)tmp[2];
  5532. out[3] = (nk_byte)tmp[3];
  5533. }
  5534. NK_API void
  5535. nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
  5536. {
  5537. int tmp[4];
  5538. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  5539. *h = (nk_byte)tmp[0];
  5540. *s = (nk_byte)tmp[1];
  5541. *v = (nk_byte)tmp[2];
  5542. *a = (nk_byte)tmp[3];
  5543. }
  5544. NK_API void
  5545. nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
  5546. {
  5547. int a;
  5548. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  5549. }
  5550. NK_API void
  5551. nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
  5552. {
  5553. int tmp[4];
  5554. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  5555. *out_h = (nk_byte)tmp[0];
  5556. *out_s = (nk_byte)tmp[1];
  5557. *out_v = (nk_byte)tmp[2];
  5558. }
  5559. NK_API void
  5560. nk_color_hsv_iv(int *out, struct nk_color in)
  5561. {
  5562. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  5563. }
  5564. NK_API void
  5565. nk_color_hsv_bv(nk_byte *out, struct nk_color in)
  5566. {
  5567. int tmp[4];
  5568. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  5569. out[0] = (nk_byte)tmp[0];
  5570. out[1] = (nk_byte)tmp[1];
  5571. out[2] = (nk_byte)tmp[2];
  5572. }
  5573. /*
  5574. * ==============================================================
  5575. *
  5576. * IMAGE
  5577. *
  5578. * ===============================================================
  5579. */
  5580. NK_API nk_handle
  5581. nk_handle_ptr(void *ptr)
  5582. {
  5583. nk_handle handle = {0};
  5584. handle.ptr = ptr;
  5585. return handle;
  5586. }
  5587. NK_API nk_handle
  5588. nk_handle_id(int id)
  5589. {
  5590. nk_handle handle;
  5591. nk_zero_struct(handle);
  5592. handle.id = id;
  5593. return handle;
  5594. }
  5595. NK_API struct nk_image
  5596. nk_subimage_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r)
  5597. {
  5598. struct nk_image s;
  5599. nk_zero(&s, sizeof(s));
  5600. s.handle.ptr = ptr;
  5601. s.w = w; s.h = h;
  5602. s.region[0] = (unsigned short)r.x;
  5603. s.region[1] = (unsigned short)r.y;
  5604. s.region[2] = (unsigned short)r.w;
  5605. s.region[3] = (unsigned short)r.h;
  5606. return s;
  5607. }
  5608. NK_API struct nk_image
  5609. nk_subimage_id(int id, unsigned short w, unsigned short h, struct nk_rect r)
  5610. {
  5611. struct nk_image s;
  5612. nk_zero(&s, sizeof(s));
  5613. s.handle.id = id;
  5614. s.w = w; s.h = h;
  5615. s.region[0] = (unsigned short)r.x;
  5616. s.region[1] = (unsigned short)r.y;
  5617. s.region[2] = (unsigned short)r.w;
  5618. s.region[3] = (unsigned short)r.h;
  5619. return s;
  5620. }
  5621. NK_API struct nk_image
  5622. nk_subimage_handle(nk_handle handle, unsigned short w, unsigned short h,
  5623. struct nk_rect r)
  5624. {
  5625. struct nk_image s;
  5626. nk_zero(&s, sizeof(s));
  5627. s.handle = handle;
  5628. s.w = w; s.h = h;
  5629. s.region[0] = (unsigned short)r.x;
  5630. s.region[1] = (unsigned short)r.y;
  5631. s.region[2] = (unsigned short)r.w;
  5632. s.region[3] = (unsigned short)r.h;
  5633. return s;
  5634. }
  5635. NK_API struct nk_image
  5636. nk_image_handle(nk_handle handle)
  5637. {
  5638. struct nk_image s;
  5639. nk_zero(&s, sizeof(s));
  5640. s.handle = handle;
  5641. s.w = 0; s.h = 0;
  5642. s.region[0] = 0;
  5643. s.region[1] = 0;
  5644. s.region[2] = 0;
  5645. s.region[3] = 0;
  5646. return s;
  5647. }
  5648. NK_API struct nk_image
  5649. nk_image_ptr(void *ptr)
  5650. {
  5651. struct nk_image s;
  5652. nk_zero(&s, sizeof(s));
  5653. NK_ASSERT(ptr);
  5654. s.handle.ptr = ptr;
  5655. s.w = 0; s.h = 0;
  5656. s.region[0] = 0;
  5657. s.region[1] = 0;
  5658. s.region[2] = 0;
  5659. s.region[3] = 0;
  5660. return s;
  5661. }
  5662. NK_API struct nk_image
  5663. nk_image_id(int id)
  5664. {
  5665. struct nk_image s;
  5666. nk_zero(&s, sizeof(s));
  5667. s.handle.id = id;
  5668. s.w = 0; s.h = 0;
  5669. s.region[0] = 0;
  5670. s.region[1] = 0;
  5671. s.region[2] = 0;
  5672. s.region[3] = 0;
  5673. return s;
  5674. }
  5675. NK_API int
  5676. nk_image_is_subimage(const struct nk_image* img)
  5677. {
  5678. NK_ASSERT(img);
  5679. return !(img->w == 0 && img->h == 0);
  5680. }
  5681. NK_INTERN void
  5682. nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
  5683. float x1, float y1)
  5684. {
  5685. NK_ASSERT(a);
  5686. NK_ASSERT(clip);
  5687. clip->x = NK_MAX(a->x, x0);
  5688. clip->y = NK_MAX(a->y, y0);
  5689. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  5690. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  5691. clip->w = NK_MAX(0, clip->w);
  5692. clip->h = NK_MAX(0, clip->h);
  5693. }
  5694. NK_API void
  5695. nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
  5696. float pad_x, float pad_y, enum nk_heading direction)
  5697. {
  5698. float w_half, h_half;
  5699. NK_ASSERT(result);
  5700. r.w = NK_MAX(2 * pad_x, r.w);
  5701. r.h = NK_MAX(2 * pad_y, r.h);
  5702. r.w = r.w - 2 * pad_x;
  5703. r.h = r.h - 2 * pad_y;
  5704. r.x = r.x + pad_x;
  5705. r.y = r.y + pad_y;
  5706. w_half = r.w / 2.0f;
  5707. h_half = r.h / 2.0f;
  5708. if (direction == NK_UP) {
  5709. result[0] = nk_vec2(r.x + w_half, r.y);
  5710. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  5711. result[2] = nk_vec2(r.x, r.y + r.h);
  5712. } else if (direction == NK_RIGHT) {
  5713. result[0] = nk_vec2(r.x, r.y);
  5714. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  5715. result[2] = nk_vec2(r.x, r.y + r.h);
  5716. } else if (direction == NK_DOWN) {
  5717. result[0] = nk_vec2(r.x, r.y);
  5718. result[1] = nk_vec2(r.x + r.w, r.y);
  5719. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  5720. } else {
  5721. result[0] = nk_vec2(r.x, r.y + h_half);
  5722. result[1] = nk_vec2(r.x + r.w, r.y);
  5723. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  5724. }
  5725. }
  5726. NK_INTERN int
  5727. nk_text_clamp(const struct nk_user_font *font, const char *text,
  5728. int text_len, float space, int *glyphs, float *text_width,
  5729. nk_rune *sep_list, int sep_count)
  5730. {
  5731. int i = 0;
  5732. int glyph_len = 0;
  5733. float last_width = 0;
  5734. nk_rune unicode = 0;
  5735. float width = 0;
  5736. int len = 0;
  5737. int g = 0;
  5738. float s;
  5739. int sep_len = 0;
  5740. int sep_g = 0;
  5741. float sep_width = 0;
  5742. sep_count = NK_MAX(sep_count,0);
  5743. glyph_len = nk_utf_decode(text, &unicode, text_len);
  5744. while (glyph_len && (width < space) && (len < text_len)) {
  5745. len += glyph_len;
  5746. s = font->width(font->userdata, font->height, text, len);
  5747. for (i = 0; i < sep_count; ++i) {
  5748. if (unicode != sep_list[i]) continue;
  5749. sep_width = last_width = width;
  5750. sep_g = g+1;
  5751. sep_len = len;
  5752. break;
  5753. }
  5754. if (i == sep_count){
  5755. last_width = sep_width = width;
  5756. sep_g = g+1;
  5757. }
  5758. width = s;
  5759. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  5760. g++;
  5761. }
  5762. if (len >= text_len) {
  5763. *glyphs = g;
  5764. *text_width = last_width;
  5765. return len;
  5766. } else {
  5767. *glyphs = sep_g;
  5768. *text_width = sep_width;
  5769. return (!sep_len) ? len: sep_len;
  5770. }
  5771. }
  5772. enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
  5773. NK_INTERN struct nk_vec2
  5774. nk_text_calculate_text_bounds(const struct nk_user_font *font,
  5775. const char *begin, int byte_len, float row_height, const char **remaining,
  5776. struct nk_vec2 *out_offset, int *glyphs, int op)
  5777. {
  5778. float line_height = row_height;
  5779. struct nk_vec2 text_size = nk_vec2(0,0);
  5780. float line_width = 0.0f;
  5781. float glyph_width;
  5782. int glyph_len = 0;
  5783. nk_rune unicode = 0;
  5784. int text_len = 0;
  5785. if (!begin || byte_len <= 0 || !font)
  5786. return nk_vec2(0,row_height);
  5787. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  5788. if (!glyph_len) return text_size;
  5789. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  5790. *glyphs = 0;
  5791. while ((text_len < byte_len) && glyph_len) {
  5792. if (unicode == '\n') {
  5793. text_size.x = NK_MAX(text_size.x, line_width);
  5794. text_size.y += line_height;
  5795. line_width = 0;
  5796. *glyphs+=1;
  5797. if (op == NK_STOP_ON_NEW_LINE)
  5798. break;
  5799. text_len++;
  5800. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  5801. continue;
  5802. }
  5803. if (unicode == '\r') {
  5804. text_len++;
  5805. *glyphs+=1;
  5806. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  5807. continue;
  5808. }
  5809. *glyphs = *glyphs + 1;
  5810. text_len += glyph_len;
  5811. line_width += (float)glyph_width;
  5812. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  5813. glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
  5814. continue;
  5815. }
  5816. if (text_size.x < line_width)
  5817. text_size.x = line_width;
  5818. if (out_offset)
  5819. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  5820. if (line_width > 0 || text_size.y == 0.0f)
  5821. text_size.y += line_height;
  5822. if (remaining)
  5823. *remaining = begin+text_len;
  5824. return text_size;
  5825. }
  5826. /* ==============================================================
  5827. *
  5828. * UTF-8
  5829. *
  5830. * ===============================================================*/
  5831. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  5832. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  5833. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
  5834. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  5835. NK_INTERN int
  5836. nk_utf_validate(nk_rune *u, int i)
  5837. {
  5838. NK_ASSERT(u);
  5839. if (!u) return 0;
  5840. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  5841. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  5842. *u = NK_UTF_INVALID;
  5843. for (i = 1; *u > nk_utfmax[i]; ++i);
  5844. return i;
  5845. }
  5846. NK_INTERN nk_rune
  5847. nk_utf_decode_byte(char c, int *i)
  5848. {
  5849. NK_ASSERT(i);
  5850. if (!i) return 0;
  5851. for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  5852. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  5853. return (nk_byte)(c & ~nk_utfmask[*i]);
  5854. }
  5855. return 0;
  5856. }
  5857. NK_API int
  5858. nk_utf_decode(const char *c, nk_rune *u, int clen)
  5859. {
  5860. int i, j, len, type=0;
  5861. nk_rune udecoded;
  5862. NK_ASSERT(c);
  5863. NK_ASSERT(u);
  5864. if (!c || !u) return 0;
  5865. if (!clen) return 0;
  5866. *u = NK_UTF_INVALID;
  5867. udecoded = nk_utf_decode_byte(c[0], &len);
  5868. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  5869. return 1;
  5870. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  5871. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  5872. if (type != 0)
  5873. return j;
  5874. }
  5875. if (j < len)
  5876. return 0;
  5877. *u = udecoded;
  5878. nk_utf_validate(u, len);
  5879. return len;
  5880. }
  5881. NK_INTERN char
  5882. nk_utf_encode_byte(nk_rune u, int i)
  5883. {
  5884. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  5885. }
  5886. NK_API int
  5887. nk_utf_encode(nk_rune u, char *c, int clen)
  5888. {
  5889. int len, i;
  5890. len = nk_utf_validate(&u, 0);
  5891. if (clen < len || !len || len > NK_UTF_SIZE)
  5892. return 0;
  5893. for (i = len - 1; i != 0; --i) {
  5894. c[i] = nk_utf_encode_byte(u, 0);
  5895. u >>= 6;
  5896. }
  5897. c[0] = nk_utf_encode_byte(u, len);
  5898. return len;
  5899. }
  5900. NK_API int
  5901. nk_utf_len(const char *str, int len)
  5902. {
  5903. const char *text;
  5904. int glyphs = 0;
  5905. int text_len;
  5906. int glyph_len;
  5907. int src_len = 0;
  5908. nk_rune unicode;
  5909. NK_ASSERT(str);
  5910. if (!str || !len) return 0;
  5911. text = str;
  5912. text_len = len;
  5913. glyph_len = nk_utf_decode(text, &unicode, text_len);
  5914. while (glyph_len && src_len < len) {
  5915. glyphs++;
  5916. src_len = src_len + glyph_len;
  5917. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  5918. }
  5919. return glyphs;
  5920. }
  5921. NK_API const char*
  5922. nk_utf_at(const char *buffer, int length, int index,
  5923. nk_rune *unicode, int *len)
  5924. {
  5925. int i = 0;
  5926. int src_len = 0;
  5927. int glyph_len = 0;
  5928. const char *text;
  5929. int text_len;
  5930. NK_ASSERT(buffer);
  5931. NK_ASSERT(unicode);
  5932. NK_ASSERT(len);
  5933. if (!buffer || !unicode || !len) return 0;
  5934. if (index < 0) {
  5935. *unicode = NK_UTF_INVALID;
  5936. *len = 0;
  5937. return 0;
  5938. }
  5939. text = buffer;
  5940. text_len = length;
  5941. glyph_len = nk_utf_decode(text, unicode, text_len);
  5942. while (glyph_len) {
  5943. if (i == index) {
  5944. *len = glyph_len;
  5945. break;
  5946. }
  5947. i++;
  5948. src_len = src_len + glyph_len;
  5949. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  5950. }
  5951. if (i != index) return 0;
  5952. return buffer + src_len;
  5953. }
  5954. /* ==============================================================
  5955. *
  5956. * BUFFER
  5957. *
  5958. * ===============================================================*/
  5959. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5960. NK_INTERN void* nk_malloc(nk_handle unused, void *old,nk_size size)
  5961. {NK_UNUSED(unused); NK_UNUSED(old); return malloc(size);}
  5962. NK_INTERN void nk_mfree(nk_handle unused, void *ptr)
  5963. {NK_UNUSED(unused); free(ptr);}
  5964. NK_API void
  5965. nk_buffer_init_default(struct nk_buffer *buffer)
  5966. {
  5967. struct nk_allocator alloc;
  5968. alloc.userdata.ptr = 0;
  5969. alloc.alloc = nk_malloc;
  5970. alloc.free = nk_mfree;
  5971. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  5972. }
  5973. #endif
  5974. NK_API void
  5975. nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
  5976. nk_size initial_size)
  5977. {
  5978. NK_ASSERT(b);
  5979. NK_ASSERT(a);
  5980. NK_ASSERT(initial_size);
  5981. if (!b || !a || !initial_size) return;
  5982. nk_zero(b, sizeof(*b));
  5983. b->type = NK_BUFFER_DYNAMIC;
  5984. b->memory.ptr = a->alloc(a->userdata,0, initial_size);
  5985. b->memory.size = initial_size;
  5986. b->size = initial_size;
  5987. b->grow_factor = 2.0f;
  5988. b->pool = *a;
  5989. }
  5990. NK_API void
  5991. nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
  5992. {
  5993. NK_ASSERT(b);
  5994. NK_ASSERT(m);
  5995. NK_ASSERT(size);
  5996. if (!b || !m || !size) return;
  5997. nk_zero(b, sizeof(*b));
  5998. b->type = NK_BUFFER_FIXED;
  5999. b->memory.ptr = m;
  6000. b->memory.size = size;
  6001. b->size = size;
  6002. }
  6003. NK_INTERN void*
  6004. nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment,
  6005. enum nk_buffer_allocation_type type)
  6006. {
  6007. void *memory = 0;
  6008. switch (type) {
  6009. default:
  6010. case NK_BUFFER_MAX:
  6011. case NK_BUFFER_FRONT:
  6012. if (align) {
  6013. memory = NK_ALIGN_PTR(unaligned, align);
  6014. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  6015. } else {
  6016. memory = unaligned;
  6017. *alignment = 0;
  6018. }
  6019. break;
  6020. case NK_BUFFER_BACK:
  6021. if (align) {
  6022. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  6023. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  6024. } else {
  6025. memory = unaligned;
  6026. *alignment = 0;
  6027. }
  6028. break;
  6029. }
  6030. return memory;
  6031. }
  6032. NK_INTERN void*
  6033. nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
  6034. {
  6035. void *temp;
  6036. nk_size buffer_size;
  6037. NK_ASSERT(b);
  6038. NK_ASSERT(size);
  6039. if (!b || !size || !b->pool.alloc || !b->pool.free)
  6040. return 0;
  6041. buffer_size = b->memory.size;
  6042. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  6043. NK_ASSERT(temp);
  6044. if (!temp) return 0;
  6045. *size = capacity;
  6046. if (temp != b->memory.ptr) {
  6047. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  6048. b->pool.free(b->pool.userdata, b->memory.ptr);
  6049. }
  6050. if (b->size == buffer_size) {
  6051. /* no back buffer so just set correct size */
  6052. b->size = capacity;
  6053. return temp;
  6054. } else {
  6055. /* copy back buffer to the end of the new buffer */
  6056. void *dst, *src;
  6057. nk_size back_size;
  6058. back_size = buffer_size - b->size;
  6059. dst = nk_ptr_add(void, temp, capacity - back_size);
  6060. src = nk_ptr_add(void, temp, b->size);
  6061. NK_MEMCPY(dst, src, back_size);
  6062. b->size = capacity - back_size;
  6063. }
  6064. return temp;
  6065. }
  6066. NK_INTERN void*
  6067. nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  6068. nk_size size, nk_size align)
  6069. {
  6070. int full;
  6071. nk_size alignment;
  6072. void *unaligned;
  6073. void *memory;
  6074. NK_ASSERT(b);
  6075. NK_ASSERT(size);
  6076. if (!b || !size) return 0;
  6077. b->needed += size;
  6078. /* calculate total size with needed alignment + size */
  6079. if (type == NK_BUFFER_FRONT)
  6080. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  6081. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  6082. memory = nk_buffer_align(unaligned, align, &alignment, type);
  6083. /* check if buffer has enough memory*/
  6084. if (type == NK_BUFFER_FRONT)
  6085. full = ((b->allocated + size + alignment) > b->size);
  6086. else full = ((b->size - NK_MIN(b->size,(size + alignment))) <= b->allocated);
  6087. if (full) {
  6088. nk_size capacity;
  6089. if (b->type != NK_BUFFER_DYNAMIC)
  6090. return 0;
  6091. NK_ASSERT(b->pool.alloc && b->pool.free);
  6092. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  6093. return 0;
  6094. /* buffer is full so allocate bigger buffer if dynamic */
  6095. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  6096. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  6097. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  6098. if (!b->memory.ptr) return 0;
  6099. /* align newly allocated pointer */
  6100. if (type == NK_BUFFER_FRONT)
  6101. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  6102. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  6103. memory = nk_buffer_align(unaligned, align, &alignment, type);
  6104. }
  6105. if (type == NK_BUFFER_FRONT)
  6106. b->allocated += size + alignment;
  6107. else b->size -= (size + alignment);
  6108. b->needed += alignment;
  6109. b->calls++;
  6110. return memory;
  6111. }
  6112. NK_API void
  6113. nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  6114. const void *memory, nk_size size, nk_size align)
  6115. {
  6116. void *mem = nk_buffer_alloc(b, type, size, align);
  6117. if (!mem) return;
  6118. NK_MEMCPY(mem, memory, size);
  6119. }
  6120. NK_API void
  6121. nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  6122. {
  6123. NK_ASSERT(buffer);
  6124. if (!buffer) return;
  6125. buffer->marker[type].active = nk_true;
  6126. if (type == NK_BUFFER_BACK)
  6127. buffer->marker[type].offset = buffer->size;
  6128. else buffer->marker[type].offset = buffer->allocated;
  6129. }
  6130. NK_API void
  6131. nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  6132. {
  6133. NK_ASSERT(buffer);
  6134. if (!buffer) return;
  6135. if (type == NK_BUFFER_BACK) {
  6136. /* reset back buffer either back to marker or empty */
  6137. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  6138. if (buffer->marker[type].active)
  6139. buffer->size = buffer->marker[type].offset;
  6140. else buffer->size = buffer->memory.size;
  6141. buffer->marker[type].active = nk_false;
  6142. } else {
  6143. /* reset front buffer either back to back marker or empty */
  6144. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  6145. if (buffer->marker[type].active)
  6146. buffer->allocated = buffer->marker[type].offset;
  6147. else buffer->allocated = 0;
  6148. buffer->marker[type].active = nk_false;
  6149. }
  6150. }
  6151. NK_API void
  6152. nk_buffer_clear(struct nk_buffer *b)
  6153. {
  6154. NK_ASSERT(b);
  6155. if (!b) return;
  6156. b->allocated = 0;
  6157. b->size = b->memory.size;
  6158. b->calls = 0;
  6159. b->needed = 0;
  6160. }
  6161. NK_API void
  6162. nk_buffer_free(struct nk_buffer *b)
  6163. {
  6164. NK_ASSERT(b);
  6165. if (!b || !b->memory.ptr) return;
  6166. if (b->type == NK_BUFFER_FIXED) return;
  6167. if (!b->pool.free) return;
  6168. NK_ASSERT(b->pool.free);
  6169. b->pool.free(b->pool.userdata, b->memory.ptr);
  6170. }
  6171. NK_API void
  6172. nk_buffer_info(struct nk_memory_status *s, struct nk_buffer *b)
  6173. {
  6174. NK_ASSERT(b);
  6175. NK_ASSERT(s);
  6176. if (!s || !b) return;
  6177. s->allocated = b->allocated;
  6178. s->size = b->memory.size;
  6179. s->needed = b->needed;
  6180. s->memory = b->memory.ptr;
  6181. s->calls = b->calls;
  6182. }
  6183. NK_API void*
  6184. nk_buffer_memory(struct nk_buffer *buffer)
  6185. {
  6186. NK_ASSERT(buffer);
  6187. if (!buffer) return 0;
  6188. return buffer->memory.ptr;
  6189. }
  6190. NK_API const void*
  6191. nk_buffer_memory_const(const struct nk_buffer *buffer)
  6192. {
  6193. NK_ASSERT(buffer);
  6194. if (!buffer) return 0;
  6195. return buffer->memory.ptr;
  6196. }
  6197. NK_API nk_size
  6198. nk_buffer_total(struct nk_buffer *buffer)
  6199. {
  6200. NK_ASSERT(buffer);
  6201. if (!buffer) return 0;
  6202. return buffer->memory.size;
  6203. }
  6204. /*
  6205. * ==============================================================
  6206. *
  6207. * STRING
  6208. *
  6209. * ===============================================================
  6210. */
  6211. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  6212. NK_API void
  6213. nk_str_init_default(struct nk_str *str)
  6214. {
  6215. struct nk_allocator alloc;
  6216. alloc.userdata.ptr = 0;
  6217. alloc.alloc = nk_malloc;
  6218. alloc.free = nk_mfree;
  6219. nk_buffer_init(&str->buffer, &alloc, 32);
  6220. str->len = 0;
  6221. }
  6222. #endif
  6223. NK_API void
  6224. nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
  6225. {
  6226. nk_buffer_init(&str->buffer, alloc, size);
  6227. str->len = 0;
  6228. }
  6229. NK_API void
  6230. nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
  6231. {
  6232. nk_buffer_init_fixed(&str->buffer, memory, size);
  6233. str->len = 0;
  6234. }
  6235. NK_API int
  6236. nk_str_append_text_char(struct nk_str *s, const char *str, int len)
  6237. {
  6238. char *mem;
  6239. NK_ASSERT(s);
  6240. NK_ASSERT(str);
  6241. if (!s || !str || !len) return 0;
  6242. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  6243. if (!mem) return 0;
  6244. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  6245. s->len += nk_utf_len(str, len);
  6246. return len;
  6247. }
  6248. NK_API int
  6249. nk_str_append_str_char(struct nk_str *s, const char *str)
  6250. {
  6251. return nk_str_append_text_char(s, str, nk_strlen(str));
  6252. }
  6253. NK_API int
  6254. nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
  6255. {
  6256. int i = 0;
  6257. int byte_len = 0;
  6258. nk_rune unicode;
  6259. if (!str || !text || !len) return 0;
  6260. for (i = 0; i < len; ++i)
  6261. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  6262. nk_str_append_text_char(str, text, byte_len);
  6263. return len;
  6264. }
  6265. NK_API int
  6266. nk_str_append_str_utf8(struct nk_str *str, const char *text)
  6267. {
  6268. int runes = 0;
  6269. int byte_len = 0;
  6270. int num_runes = 0;
  6271. int glyph_len = 0;
  6272. nk_rune unicode;
  6273. if (!str || !text) return 0;
  6274. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  6275. while (unicode != '\0' && glyph_len) {
  6276. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  6277. byte_len += glyph_len;
  6278. num_runes++;
  6279. }
  6280. nk_str_append_text_char(str, text, byte_len);
  6281. return runes;
  6282. }
  6283. NK_API int
  6284. nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
  6285. {
  6286. int i = 0;
  6287. int byte_len = 0;
  6288. nk_glyph glyph;
  6289. NK_ASSERT(str);
  6290. if (!str || !text || !len) return 0;
  6291. for (i = 0; i < len; ++i) {
  6292. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  6293. if (!byte_len) break;
  6294. nk_str_append_text_char(str, glyph, byte_len);
  6295. }
  6296. return len;
  6297. }
  6298. NK_API int
  6299. nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
  6300. {
  6301. int i = 0;
  6302. nk_glyph glyph;
  6303. int byte_len;
  6304. NK_ASSERT(str);
  6305. if (!str || !runes) return 0;
  6306. while (runes[i] != '\0') {
  6307. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  6308. nk_str_append_text_char(str, glyph, byte_len);
  6309. i++;
  6310. }
  6311. return i;
  6312. }
  6313. NK_API int
  6314. nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
  6315. {
  6316. int i;
  6317. void *mem;
  6318. char *src;
  6319. char *dst;
  6320. int copylen;
  6321. NK_ASSERT(s);
  6322. NK_ASSERT(str);
  6323. NK_ASSERT(len >= 0);
  6324. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  6325. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  6326. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  6327. copylen = (int)s->buffer.allocated - pos;
  6328. if (!copylen) {
  6329. nk_str_append_text_char(s, str, len);
  6330. return 1;
  6331. }
  6332. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  6333. if (!mem) return 0;
  6334. /* memmove */
  6335. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  6336. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  6337. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  6338. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
  6339. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  6340. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  6341. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  6342. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  6343. return 1;
  6344. }
  6345. NK_API int
  6346. nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
  6347. {
  6348. int glyph_len;
  6349. nk_rune unicode;
  6350. const char *begin;
  6351. const char *buffer;
  6352. NK_ASSERT(str);
  6353. NK_ASSERT(cstr);
  6354. NK_ASSERT(len);
  6355. if (!str || !cstr || !len) return 0;
  6356. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  6357. if (!str->len)
  6358. return nk_str_append_text_char(str, cstr, len);
  6359. buffer = nk_str_get_const(str);
  6360. if (!begin) return 0;
  6361. return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
  6362. }
  6363. NK_API int
  6364. nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
  6365. {
  6366. return nk_str_insert_text_utf8(str, pos, text, len);
  6367. }
  6368. NK_API int
  6369. nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
  6370. {
  6371. return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
  6372. }
  6373. NK_API int
  6374. nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
  6375. {
  6376. int i = 0;
  6377. int byte_len = 0;
  6378. nk_rune unicode;
  6379. NK_ASSERT(str);
  6380. NK_ASSERT(text);
  6381. if (!str || !text || !len) return 0;
  6382. for (i = 0; i < len; ++i)
  6383. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  6384. nk_str_insert_at_rune(str, pos, text, byte_len);
  6385. return len;
  6386. }
  6387. NK_API int
  6388. nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
  6389. {
  6390. int runes = 0;
  6391. int byte_len = 0;
  6392. int num_runes = 0;
  6393. int glyph_len = 0;
  6394. nk_rune unicode;
  6395. if (!str || !text) return 0;
  6396. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  6397. while (unicode != '\0' && glyph_len) {
  6398. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  6399. byte_len += glyph_len;
  6400. num_runes++;
  6401. }
  6402. nk_str_insert_at_rune(str, pos, text, byte_len);
  6403. return runes;
  6404. }
  6405. NK_API int
  6406. nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
  6407. {
  6408. int i = 0;
  6409. int byte_len = 0;
  6410. nk_glyph glyph;
  6411. NK_ASSERT(str);
  6412. if (!str || !runes || !len) return 0;
  6413. for (i = 0; i < len; ++i) {
  6414. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  6415. if (!byte_len) break;
  6416. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  6417. }
  6418. return len;
  6419. }
  6420. NK_API int
  6421. nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
  6422. {
  6423. int i = 0;
  6424. nk_glyph glyph;
  6425. int byte_len;
  6426. NK_ASSERT(str);
  6427. if (!str || !runes) return 0;
  6428. while (runes[i] != '\0') {
  6429. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  6430. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  6431. i++;
  6432. }
  6433. return i;
  6434. }
  6435. NK_API void
  6436. nk_str_remove_chars(struct nk_str *s, int len)
  6437. {
  6438. NK_ASSERT(s);
  6439. NK_ASSERT(len >= 0);
  6440. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  6441. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  6442. s->buffer.allocated -= (nk_size)len;
  6443. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  6444. }
  6445. NK_API void
  6446. nk_str_remove_runes(struct nk_str *str, int len)
  6447. {
  6448. int index;
  6449. const char *begin;
  6450. const char *end;
  6451. nk_rune unicode;
  6452. NK_ASSERT(str);
  6453. NK_ASSERT(len >= 0);
  6454. if (!str || len < 0) return;
  6455. if (len >= str->len) {
  6456. str->len = 0;
  6457. return;
  6458. }
  6459. index = str->len - len;
  6460. begin = nk_str_at_rune(str, index, &unicode, &len);
  6461. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  6462. nk_str_remove_chars(str, (int)(end-begin)+1);
  6463. }
  6464. NK_API void
  6465. nk_str_delete_chars(struct nk_str *s, int pos, int len)
  6466. {
  6467. NK_ASSERT(s);
  6468. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  6469. (nk_size)(pos + len) > s->buffer.allocated) return;
  6470. if ((nk_size)(pos + len) < s->buffer.allocated) {
  6471. /* memmove */
  6472. char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  6473. char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  6474. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  6475. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  6476. s->buffer.allocated -= (nk_size)len;
  6477. } else nk_str_remove_chars(s, len);
  6478. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  6479. }
  6480. NK_API void
  6481. nk_str_delete_runes(struct nk_str *s, int pos, int len)
  6482. {
  6483. char *temp;
  6484. nk_rune unicode;
  6485. char *begin;
  6486. char *end;
  6487. int unused;
  6488. NK_ASSERT(s);
  6489. NK_ASSERT(s->len >= pos + len);
  6490. if (s->len < pos + len)
  6491. len = NK_CLAMP(0, (s->len - pos), s->len);
  6492. if (!len) return;
  6493. temp = (char *)s->buffer.memory.ptr;
  6494. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  6495. if (!begin) return;
  6496. s->buffer.memory.ptr = begin;
  6497. end = nk_str_at_rune(s, len, &unicode, &unused);
  6498. s->buffer.memory.ptr = temp;
  6499. if (!end) return;
  6500. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  6501. }
  6502. NK_API char*
  6503. nk_str_at_char(struct nk_str *s, int pos)
  6504. {
  6505. NK_ASSERT(s);
  6506. if (!s || pos > (int)s->buffer.allocated) return 0;
  6507. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  6508. }
  6509. NK_API char*
  6510. nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
  6511. {
  6512. int i = 0;
  6513. int src_len = 0;
  6514. int glyph_len = 0;
  6515. char *text;
  6516. int text_len;
  6517. NK_ASSERT(str);
  6518. NK_ASSERT(unicode);
  6519. NK_ASSERT(len);
  6520. if (!str || !unicode || !len) return 0;
  6521. if (pos < 0) {
  6522. *unicode = 0;
  6523. *len = 0;
  6524. return 0;
  6525. }
  6526. text = (char*)str->buffer.memory.ptr;
  6527. text_len = (int)str->buffer.allocated;
  6528. glyph_len = nk_utf_decode(text, unicode, text_len);
  6529. while (glyph_len) {
  6530. if (i == pos) {
  6531. *len = glyph_len;
  6532. break;
  6533. }
  6534. i++;
  6535. src_len = src_len + glyph_len;
  6536. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  6537. }
  6538. if (i != pos) return 0;
  6539. return text + src_len;
  6540. }
  6541. NK_API const char*
  6542. nk_str_at_char_const(const struct nk_str *s, int pos)
  6543. {
  6544. NK_ASSERT(s);
  6545. if (!s || pos > (int)s->buffer.allocated) return 0;
  6546. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  6547. }
  6548. NK_API const char*
  6549. nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
  6550. {
  6551. int i = 0;
  6552. int src_len = 0;
  6553. int glyph_len = 0;
  6554. char *text;
  6555. int text_len;
  6556. NK_ASSERT(str);
  6557. NK_ASSERT(unicode);
  6558. NK_ASSERT(len);
  6559. if (!str || !unicode || !len) return 0;
  6560. if (pos < 0) {
  6561. *unicode = 0;
  6562. *len = 0;
  6563. return 0;
  6564. }
  6565. text = (char*)str->buffer.memory.ptr;
  6566. text_len = (int)str->buffer.allocated;
  6567. glyph_len = nk_utf_decode(text, unicode, text_len);
  6568. while (glyph_len) {
  6569. if (i == pos) {
  6570. *len = glyph_len;
  6571. break;
  6572. }
  6573. i++;
  6574. src_len = src_len + glyph_len;
  6575. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  6576. }
  6577. if (i != pos) return 0;
  6578. return text + src_len;
  6579. }
  6580. NK_API nk_rune
  6581. nk_str_rune_at(const struct nk_str *str, int pos)
  6582. {
  6583. int len;
  6584. nk_rune unicode = 0;
  6585. nk_str_at_const(str, pos, &unicode, &len);
  6586. return unicode;
  6587. }
  6588. NK_API char*
  6589. nk_str_get(struct nk_str *s)
  6590. {
  6591. NK_ASSERT(s);
  6592. if (!s || !s->len || !s->buffer.allocated) return 0;
  6593. return (char*)s->buffer.memory.ptr;
  6594. }
  6595. NK_API const char*
  6596. nk_str_get_const(const struct nk_str *s)
  6597. {
  6598. NK_ASSERT(s);
  6599. if (!s || !s->len || !s->buffer.allocated) return 0;
  6600. return (const char*)s->buffer.memory.ptr;
  6601. }
  6602. NK_API int
  6603. nk_str_len(struct nk_str *s)
  6604. {
  6605. NK_ASSERT(s);
  6606. if (!s || !s->len || !s->buffer.allocated) return 0;
  6607. return s->len;
  6608. }
  6609. NK_API int
  6610. nk_str_len_char(struct nk_str *s)
  6611. {
  6612. NK_ASSERT(s);
  6613. if (!s || !s->len || !s->buffer.allocated) return 0;
  6614. return (int)s->buffer.allocated;
  6615. }
  6616. NK_API void
  6617. nk_str_clear(struct nk_str *str)
  6618. {
  6619. NK_ASSERT(str);
  6620. nk_buffer_clear(&str->buffer);
  6621. str->len = 0;
  6622. }
  6623. NK_API void
  6624. nk_str_free(struct nk_str *str)
  6625. {
  6626. NK_ASSERT(str);
  6627. nk_buffer_free(&str->buffer);
  6628. str->len = 0;
  6629. }
  6630. /*
  6631. * ==============================================================
  6632. *
  6633. * Command buffer
  6634. *
  6635. * ===============================================================
  6636. */
  6637. NK_INTERN void
  6638. nk_command_buffer_init(struct nk_command_buffer *cmdbuf,
  6639. struct nk_buffer *buffer, enum nk_command_clipping clip)
  6640. {
  6641. NK_ASSERT(cmdbuf);
  6642. NK_ASSERT(buffer);
  6643. if (!cmdbuf || !buffer) return;
  6644. cmdbuf->base = buffer;
  6645. cmdbuf->use_clipping = clip;
  6646. cmdbuf->begin = buffer->allocated;
  6647. cmdbuf->end = buffer->allocated;
  6648. cmdbuf->last = buffer->allocated;
  6649. }
  6650. NK_INTERN void
  6651. nk_command_buffer_reset(struct nk_command_buffer *buffer)
  6652. {
  6653. NK_ASSERT(buffer);
  6654. if (!buffer) return;
  6655. buffer->begin = 0;
  6656. buffer->end = 0;
  6657. buffer->last = 0;
  6658. buffer->clip = nk_null_rect;
  6659. #ifdef NK_INCLUDE_COMMAND_USERDATA
  6660. buffer->userdata.ptr = 0;
  6661. #endif
  6662. }
  6663. NK_INTERN void*
  6664. nk_command_buffer_push(struct nk_command_buffer* b,
  6665. enum nk_command_type t, nk_size size)
  6666. {
  6667. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  6668. struct nk_command *cmd;
  6669. nk_size alignment;
  6670. void *unaligned;
  6671. void *memory;
  6672. NK_ASSERT(b);
  6673. NK_ASSERT(b->base);
  6674. if (!b) return 0;
  6675. cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
  6676. if (!cmd) return 0;
  6677. /* make sure the offset to the next command is aligned */
  6678. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  6679. unaligned = (nk_byte*)cmd + size;
  6680. memory = NK_ALIGN_PTR(unaligned, align);
  6681. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  6682. #ifdef NK_ZERO_COMMAND_MEMORY
  6683. NK_MEMSET(cmd, 0, size + alignment);
  6684. #endif
  6685. cmd->type = t;
  6686. cmd->next = b->base->allocated + alignment;
  6687. #ifdef NK_INCLUDE_COMMAND_USERDATA
  6688. cmd->userdata = b->userdata;
  6689. #endif
  6690. b->end = cmd->next;
  6691. return cmd;
  6692. }
  6693. NK_API void
  6694. nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
  6695. {
  6696. struct nk_command_scissor *cmd;
  6697. NK_ASSERT(b);
  6698. if (!b) return;
  6699. b->clip.x = r.x;
  6700. b->clip.y = r.y;
  6701. b->clip.w = r.w;
  6702. b->clip.h = r.h;
  6703. cmd = (struct nk_command_scissor*)
  6704. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  6705. if (!cmd) return;
  6706. cmd->x = (short)r.x;
  6707. cmd->y = (short)r.y;
  6708. cmd->w = (unsigned short)NK_MAX(0, r.w);
  6709. cmd->h = (unsigned short)NK_MAX(0, r.h);
  6710. }
  6711. NK_API void
  6712. nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
  6713. float x1, float y1, float line_thickness, struct nk_color c)
  6714. {
  6715. struct nk_command_line *cmd;
  6716. NK_ASSERT(b);
  6717. if (!b || line_thickness <= 0) return;
  6718. cmd = (struct nk_command_line*)
  6719. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  6720. if (!cmd) return;
  6721. cmd->line_thickness = (unsigned short)line_thickness;
  6722. cmd->begin.x = (short)x0;
  6723. cmd->begin.y = (short)y0;
  6724. cmd->end.x = (short)x1;
  6725. cmd->end.y = (short)y1;
  6726. cmd->color = c;
  6727. }
  6728. NK_API void
  6729. nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
  6730. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  6731. float bx, float by, float line_thickness, struct nk_color col)
  6732. {
  6733. struct nk_command_curve *cmd;
  6734. NK_ASSERT(b);
  6735. if (!b || col.a == 0 || line_thickness <= 0) return;
  6736. cmd = (struct nk_command_curve*)
  6737. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  6738. if (!cmd) return;
  6739. cmd->line_thickness = (unsigned short)line_thickness;
  6740. cmd->begin.x = (short)ax;
  6741. cmd->begin.y = (short)ay;
  6742. cmd->ctrl[0].x = (short)ctrl0x;
  6743. cmd->ctrl[0].y = (short)ctrl0y;
  6744. cmd->ctrl[1].x = (short)ctrl1x;
  6745. cmd->ctrl[1].y = (short)ctrl1y;
  6746. cmd->end.x = (short)bx;
  6747. cmd->end.y = (short)by;
  6748. cmd->color = col;
  6749. }
  6750. NK_API void
  6751. nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
  6752. float rounding, float line_thickness, struct nk_color c)
  6753. {
  6754. struct nk_command_rect *cmd;
  6755. NK_ASSERT(b);
  6756. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
  6757. if (b->use_clipping) {
  6758. const struct nk_rect *clip = &b->clip;
  6759. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  6760. clip->x, clip->y, clip->w, clip->h)) return;
  6761. }
  6762. cmd = (struct nk_command_rect*)
  6763. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  6764. if (!cmd) return;
  6765. cmd->rounding = (unsigned short)rounding;
  6766. cmd->line_thickness = (unsigned short)line_thickness;
  6767. cmd->x = (short)rect.x;
  6768. cmd->y = (short)rect.y;
  6769. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  6770. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  6771. cmd->color = c;
  6772. }
  6773. NK_API void
  6774. nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
  6775. float rounding, struct nk_color c)
  6776. {
  6777. struct nk_command_rect_filled *cmd;
  6778. NK_ASSERT(b);
  6779. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
  6780. if (b->use_clipping) {
  6781. const struct nk_rect *clip = &b->clip;
  6782. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  6783. clip->x, clip->y, clip->w, clip->h)) return;
  6784. }
  6785. cmd = (struct nk_command_rect_filled*)
  6786. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  6787. if (!cmd) return;
  6788. cmd->rounding = (unsigned short)rounding;
  6789. cmd->x = (short)rect.x;
  6790. cmd->y = (short)rect.y;
  6791. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  6792. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  6793. cmd->color = c;
  6794. }
  6795. NK_API void
  6796. nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
  6797. struct nk_color left, struct nk_color top, struct nk_color right,
  6798. struct nk_color bottom)
  6799. {
  6800. struct nk_command_rect_multi_color *cmd;
  6801. NK_ASSERT(b);
  6802. if (!b || rect.w == 0 || rect.h == 0) return;
  6803. if (b->use_clipping) {
  6804. const struct nk_rect *clip = &b->clip;
  6805. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  6806. clip->x, clip->y, clip->w, clip->h)) return;
  6807. }
  6808. cmd = (struct nk_command_rect_multi_color*)
  6809. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  6810. if (!cmd) return;
  6811. cmd->x = (short)rect.x;
  6812. cmd->y = (short)rect.y;
  6813. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  6814. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  6815. cmd->left = left;
  6816. cmd->top = top;
  6817. cmd->right = right;
  6818. cmd->bottom = bottom;
  6819. }
  6820. NK_API void
  6821. nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
  6822. float line_thickness, struct nk_color c)
  6823. {
  6824. struct nk_command_circle *cmd;
  6825. if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
  6826. if (b->use_clipping) {
  6827. const struct nk_rect *clip = &b->clip;
  6828. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  6829. return;
  6830. }
  6831. cmd = (struct nk_command_circle*)
  6832. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  6833. if (!cmd) return;
  6834. cmd->line_thickness = (unsigned short)line_thickness;
  6835. cmd->x = (short)r.x;
  6836. cmd->y = (short)r.y;
  6837. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  6838. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  6839. cmd->color = c;
  6840. }
  6841. NK_API void
  6842. nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
  6843. {
  6844. struct nk_command_circle_filled *cmd;
  6845. NK_ASSERT(b);
  6846. if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
  6847. if (b->use_clipping) {
  6848. const struct nk_rect *clip = &b->clip;
  6849. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  6850. return;
  6851. }
  6852. cmd = (struct nk_command_circle_filled*)
  6853. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  6854. if (!cmd) return;
  6855. cmd->x = (short)r.x;
  6856. cmd->y = (short)r.y;
  6857. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  6858. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  6859. cmd->color = c;
  6860. }
  6861. NK_API void
  6862. nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  6863. float a_min, float a_max, float line_thickness, struct nk_color c)
  6864. {
  6865. struct nk_command_arc *cmd;
  6866. if (!b || c.a == 0 || line_thickness <= 0) return;
  6867. cmd = (struct nk_command_arc*)
  6868. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  6869. if (!cmd) return;
  6870. cmd->line_thickness = (unsigned short)line_thickness;
  6871. cmd->cx = (short)cx;
  6872. cmd->cy = (short)cy;
  6873. cmd->r = (unsigned short)radius;
  6874. cmd->a[0] = a_min;
  6875. cmd->a[1] = a_max;
  6876. cmd->color = c;
  6877. }
  6878. NK_API void
  6879. nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  6880. float a_min, float a_max, struct nk_color c)
  6881. {
  6882. struct nk_command_arc_filled *cmd;
  6883. NK_ASSERT(b);
  6884. if (!b || c.a == 0) return;
  6885. cmd = (struct nk_command_arc_filled*)
  6886. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  6887. if (!cmd) return;
  6888. cmd->cx = (short)cx;
  6889. cmd->cy = (short)cy;
  6890. cmd->r = (unsigned short)radius;
  6891. cmd->a[0] = a_min;
  6892. cmd->a[1] = a_max;
  6893. cmd->color = c;
  6894. }
  6895. NK_API void
  6896. nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  6897. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  6898. {
  6899. struct nk_command_triangle *cmd;
  6900. NK_ASSERT(b);
  6901. if (!b || c.a == 0 || line_thickness <= 0) return;
  6902. if (b->use_clipping) {
  6903. const struct nk_rect *clip = &b->clip;
  6904. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  6905. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  6906. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  6907. return;
  6908. }
  6909. cmd = (struct nk_command_triangle*)
  6910. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  6911. if (!cmd) return;
  6912. cmd->line_thickness = (unsigned short)line_thickness;
  6913. cmd->a.x = (short)x0;
  6914. cmd->a.y = (short)y0;
  6915. cmd->b.x = (short)x1;
  6916. cmd->b.y = (short)y1;
  6917. cmd->c.x = (short)x2;
  6918. cmd->c.y = (short)y2;
  6919. cmd->color = c;
  6920. }
  6921. NK_API void
  6922. nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  6923. float y1, float x2, float y2, struct nk_color c)
  6924. {
  6925. struct nk_command_triangle_filled *cmd;
  6926. NK_ASSERT(b);
  6927. if (!b || c.a == 0) return;
  6928. if (!b) return;
  6929. if (b->use_clipping) {
  6930. const struct nk_rect *clip = &b->clip;
  6931. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  6932. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  6933. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  6934. return;
  6935. }
  6936. cmd = (struct nk_command_triangle_filled*)
  6937. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  6938. if (!cmd) return;
  6939. cmd->a.x = (short)x0;
  6940. cmd->a.y = (short)y0;
  6941. cmd->b.x = (short)x1;
  6942. cmd->b.y = (short)y1;
  6943. cmd->c.x = (short)x2;
  6944. cmd->c.y = (short)y2;
  6945. cmd->color = c;
  6946. }
  6947. NK_API void
  6948. nk_stroke_polygon(struct nk_command_buffer *b, float *points, int point_count,
  6949. float line_thickness, struct nk_color col)
  6950. {
  6951. int i;
  6952. nk_size size = 0;
  6953. struct nk_command_polygon *cmd;
  6954. NK_ASSERT(b);
  6955. if (!b || col.a == 0 || line_thickness <= 0) return;
  6956. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  6957. cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  6958. if (!cmd) return;
  6959. cmd->color = col;
  6960. cmd->line_thickness = (unsigned short)line_thickness;
  6961. cmd->point_count = (unsigned short)point_count;
  6962. for (i = 0; i < point_count; ++i) {
  6963. cmd->points[i].x = (short)points[i*2];
  6964. cmd->points[i].y = (short)points[i*2+1];
  6965. }
  6966. }
  6967. NK_API void
  6968. nk_fill_polygon(struct nk_command_buffer *b, float *points, int point_count,
  6969. struct nk_color col)
  6970. {
  6971. int i;
  6972. nk_size size = 0;
  6973. struct nk_command_polygon_filled *cmd;
  6974. NK_ASSERT(b);
  6975. if (!b || col.a == 0) return;
  6976. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  6977. cmd = (struct nk_command_polygon_filled*)
  6978. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  6979. if (!cmd) return;
  6980. cmd->color = col;
  6981. cmd->point_count = (unsigned short)point_count;
  6982. for (i = 0; i < point_count; ++i) {
  6983. cmd->points[i].x = (short)points[i*2+0];
  6984. cmd->points[i].y = (short)points[i*2+1];
  6985. }
  6986. }
  6987. NK_API void
  6988. nk_stroke_polyline(struct nk_command_buffer *b, float *points, int point_count,
  6989. float line_thickness, struct nk_color col)
  6990. {
  6991. int i;
  6992. nk_size size = 0;
  6993. struct nk_command_polyline *cmd;
  6994. NK_ASSERT(b);
  6995. if (!b || col.a == 0 || line_thickness <= 0) return;
  6996. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  6997. cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  6998. if (!cmd) return;
  6999. cmd->color = col;
  7000. cmd->point_count = (unsigned short)point_count;
  7001. cmd->line_thickness = (unsigned short)line_thickness;
  7002. for (i = 0; i < point_count; ++i) {
  7003. cmd->points[i].x = (short)points[i*2];
  7004. cmd->points[i].y = (short)points[i*2+1];
  7005. }
  7006. }
  7007. NK_API void
  7008. nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
  7009. const struct nk_image *img, struct nk_color col)
  7010. {
  7011. struct nk_command_image *cmd;
  7012. NK_ASSERT(b);
  7013. if (!b) return;
  7014. if (b->use_clipping) {
  7015. const struct nk_rect *c = &b->clip;
  7016. 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))
  7017. return;
  7018. }
  7019. cmd = (struct nk_command_image*)
  7020. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  7021. if (!cmd) return;
  7022. cmd->x = (short)r.x;
  7023. cmd->y = (short)r.y;
  7024. cmd->w = (unsigned short)NK_MAX(0, r.w);
  7025. cmd->h = (unsigned short)NK_MAX(0, r.h);
  7026. cmd->img = *img;
  7027. cmd->col = col;
  7028. }
  7029. NK_API void
  7030. nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
  7031. nk_command_custom_callback cb, nk_handle usr)
  7032. {
  7033. struct nk_command_custom *cmd;
  7034. NK_ASSERT(b);
  7035. if (!b) return;
  7036. if (b->use_clipping) {
  7037. const struct nk_rect *c = &b->clip;
  7038. 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))
  7039. return;
  7040. }
  7041. cmd = (struct nk_command_custom*)
  7042. nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
  7043. if (!cmd) return;
  7044. cmd->x = (short)r.x;
  7045. cmd->y = (short)r.y;
  7046. cmd->w = (unsigned short)NK_MAX(0, r.w);
  7047. cmd->h = (unsigned short)NK_MAX(0, r.h);
  7048. cmd->callback_data = usr;
  7049. cmd->callback = cb;
  7050. }
  7051. NK_API void
  7052. nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
  7053. const char *string, int length, const struct nk_user_font *font,
  7054. struct nk_color bg, struct nk_color fg)
  7055. {
  7056. float text_width = 0;
  7057. struct nk_command_text *cmd;
  7058. NK_ASSERT(b);
  7059. NK_ASSERT(font);
  7060. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  7061. if (b->use_clipping) {
  7062. const struct nk_rect *c = &b->clip;
  7063. 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))
  7064. return;
  7065. }
  7066. /* make sure text fits inside bounds */
  7067. text_width = font->width(font->userdata, font->height, string, length);
  7068. if (text_width > r.w){
  7069. int glyphs = 0;
  7070. float txt_width = (float)text_width;
  7071. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
  7072. }
  7073. if (!length) return;
  7074. cmd = (struct nk_command_text*)
  7075. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  7076. if (!cmd) return;
  7077. cmd->x = (short)r.x;
  7078. cmd->y = (short)r.y;
  7079. cmd->w = (unsigned short)r.w;
  7080. cmd->h = (unsigned short)r.h;
  7081. cmd->background = bg;
  7082. cmd->foreground = fg;
  7083. cmd->font = font;
  7084. cmd->length = length;
  7085. cmd->height = font->height;
  7086. NK_MEMCPY(cmd->string, string, (nk_size)length);
  7087. cmd->string[length] = '\0';
  7088. }
  7089. /* ==============================================================
  7090. *
  7091. * DRAW LIST
  7092. *
  7093. * ===============================================================*/
  7094. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  7095. NK_API void
  7096. nk_draw_list_init(struct nk_draw_list *list)
  7097. {
  7098. nk_size i = 0;
  7099. NK_ASSERT(list);
  7100. if (!list) return;
  7101. nk_zero(list, sizeof(*list));
  7102. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  7103. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  7104. list->circle_vtx[i].x = (float)NK_COS(a);
  7105. list->circle_vtx[i].y = (float)NK_SIN(a);
  7106. }
  7107. }
  7108. NK_API void
  7109. nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
  7110. struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
  7111. enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
  7112. {
  7113. NK_ASSERT(canvas);
  7114. NK_ASSERT(config);
  7115. NK_ASSERT(cmds);
  7116. NK_ASSERT(vertices);
  7117. NK_ASSERT(elements);
  7118. if (!canvas || !config || !cmds || !vertices || !elements)
  7119. return;
  7120. canvas->buffer = cmds;
  7121. canvas->config = *config;
  7122. canvas->elements = elements;
  7123. canvas->vertices = vertices;
  7124. canvas->line_AA = line_aa;
  7125. canvas->shape_AA = shape_aa;
  7126. canvas->clip_rect = nk_null_rect;
  7127. }
  7128. NK_API const struct nk_draw_command*
  7129. nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  7130. {
  7131. nk_byte *memory;
  7132. nk_size offset;
  7133. const struct nk_draw_command *cmd;
  7134. NK_ASSERT(buffer);
  7135. if (!buffer || !buffer->size || !canvas->cmd_count)
  7136. return 0;
  7137. memory = (nk_byte*)buffer->memory.ptr;
  7138. offset = buffer->memory.size - canvas->cmd_offset;
  7139. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  7140. return cmd;
  7141. }
  7142. NK_API const struct nk_draw_command*
  7143. nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  7144. {
  7145. nk_size size;
  7146. nk_size offset;
  7147. nk_byte *memory;
  7148. const struct nk_draw_command *end;
  7149. NK_ASSERT(buffer);
  7150. NK_ASSERT(canvas);
  7151. if (!buffer || !canvas)
  7152. return 0;
  7153. memory = (nk_byte*)buffer->memory.ptr;
  7154. size = buffer->memory.size;
  7155. offset = size - canvas->cmd_offset;
  7156. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  7157. end -= (canvas->cmd_count-1);
  7158. return end;
  7159. }
  7160. NK_API const struct nk_draw_command*
  7161. nk__draw_list_next(const struct nk_draw_command *cmd,
  7162. const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
  7163. {
  7164. const struct nk_draw_command *end;
  7165. NK_ASSERT(buffer);
  7166. NK_ASSERT(canvas);
  7167. if (!cmd || !buffer || !canvas)
  7168. return 0;
  7169. end = nk__draw_list_end(canvas, buffer);
  7170. if (cmd <= end) return 0;
  7171. return (cmd-1);
  7172. }
  7173. NK_API void
  7174. nk_draw_list_clear(struct nk_draw_list *list)
  7175. {
  7176. NK_ASSERT(list);
  7177. if (!list) return;
  7178. if (list->buffer)
  7179. nk_buffer_clear(list->buffer);
  7180. if (list->vertices)
  7181. nk_buffer_clear(list->vertices);
  7182. if (list->elements)
  7183. nk_buffer_clear(list->elements);
  7184. list->element_count = 0;
  7185. list->vertex_count = 0;
  7186. list->cmd_offset = 0;
  7187. list->cmd_count = 0;
  7188. list->path_count = 0;
  7189. list->vertices = 0;
  7190. list->elements = 0;
  7191. list->clip_rect = nk_null_rect;
  7192. }
  7193. NK_INTERN struct nk_vec2*
  7194. nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
  7195. {
  7196. struct nk_vec2 *points;
  7197. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  7198. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  7199. points = (struct nk_vec2*)
  7200. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  7201. point_size * (nk_size)count, point_align);
  7202. if (!points) return 0;
  7203. if (!list->path_offset) {
  7204. void *memory = nk_buffer_memory(list->buffer);
  7205. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  7206. }
  7207. list->path_count += (unsigned int)count;
  7208. return points;
  7209. }
  7210. NK_INTERN struct nk_vec2
  7211. nk_draw_list_path_last(struct nk_draw_list *list)
  7212. {
  7213. void *memory;
  7214. struct nk_vec2 *point;
  7215. NK_ASSERT(list->path_count);
  7216. memory = nk_buffer_memory(list->buffer);
  7217. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  7218. point += (list->path_count-1);
  7219. return *point;
  7220. }
  7221. NK_INTERN struct nk_draw_command*
  7222. nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
  7223. nk_handle texture)
  7224. {
  7225. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  7226. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  7227. struct nk_draw_command *cmd;
  7228. NK_ASSERT(list);
  7229. cmd = (struct nk_draw_command*)
  7230. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  7231. if (!cmd) return 0;
  7232. if (!list->cmd_count) {
  7233. nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
  7234. nk_size total = nk_buffer_total(list->buffer);
  7235. memory = nk_ptr_add(nk_byte, memory, total);
  7236. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  7237. }
  7238. cmd->elem_count = 0;
  7239. cmd->clip_rect = clip;
  7240. cmd->texture = texture;
  7241. #ifdef NK_INCLUDE_COMMAND_USERDATA
  7242. cmd->userdata = list->userdata;
  7243. #endif
  7244. list->cmd_count++;
  7245. list->clip_rect = clip;
  7246. return cmd;
  7247. }
  7248. NK_INTERN struct nk_draw_command*
  7249. nk_draw_list_command_last(struct nk_draw_list *list)
  7250. {
  7251. void *memory;
  7252. nk_size size;
  7253. struct nk_draw_command *cmd;
  7254. NK_ASSERT(list->cmd_count);
  7255. memory = nk_buffer_memory(list->buffer);
  7256. size = nk_buffer_total(list->buffer);
  7257. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  7258. return (cmd - (list->cmd_count-1));
  7259. }
  7260. NK_INTERN void
  7261. nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
  7262. {
  7263. NK_ASSERT(list);
  7264. if (!list) return;
  7265. if (!list->cmd_count) {
  7266. nk_draw_list_push_command(list, rect, list->config.null.texture);
  7267. } else {
  7268. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  7269. if (prev->elem_count == 0)
  7270. prev->clip_rect = rect;
  7271. nk_draw_list_push_command(list, rect, prev->texture);
  7272. }
  7273. }
  7274. NK_INTERN void
  7275. nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
  7276. {
  7277. NK_ASSERT(list);
  7278. if (!list) return;
  7279. if (!list->cmd_count) {
  7280. nk_draw_list_push_command(list, nk_null_rect, texture);
  7281. } else {
  7282. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  7283. if (prev->elem_count == 0)
  7284. prev->texture = texture;
  7285. else if (prev->texture.id != texture.id)
  7286. nk_draw_list_push_command(list, prev->clip_rect, texture);
  7287. }
  7288. }
  7289. #ifdef NK_INCLUDE_COMMAND_USERDATA
  7290. NK_API void
  7291. nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
  7292. {
  7293. list->userdata = userdata;
  7294. }
  7295. #endif
  7296. NK_INTERN void*
  7297. nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
  7298. {
  7299. void *vtx;
  7300. NK_ASSERT(list);
  7301. if (!list) return 0;
  7302. vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
  7303. list->config.vertex_size*count, list->config.vertex_alignment);
  7304. if (!vtx) return 0;
  7305. list->vertex_count += (unsigned int)count;
  7306. return vtx;
  7307. }
  7308. NK_INTERN nk_draw_index*
  7309. nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
  7310. {
  7311. nk_draw_index *ids;
  7312. struct nk_draw_command *cmd;
  7313. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  7314. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  7315. NK_ASSERT(list);
  7316. if (!list) return 0;
  7317. ids = (nk_draw_index*)
  7318. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
  7319. if (!ids) return 0;
  7320. cmd = nk_draw_list_command_last(list);
  7321. list->element_count += (unsigned int)count;
  7322. cmd->elem_count += (unsigned int)count;
  7323. return ids;
  7324. }
  7325. NK_INTERN int
  7326. nk_draw_vertex_layout_element_is_end_of_layout(
  7327. const struct nk_draw_vertex_layout_element *element)
  7328. {
  7329. return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
  7330. element->format == NK_FORMAT_COUNT);
  7331. }
  7332. NK_INTERN void
  7333. nk_draw_vertex_color(void *attribute, const float *values,
  7334. enum nk_draw_vertex_layout_format format)
  7335. {
  7336. /* if this triggers you tried to provide a value format for a color */
  7337. NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
  7338. NK_ASSERT(format <= NK_FORMAT_COLOR_END);
  7339. if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
  7340. switch (format) {
  7341. default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
  7342. case NK_FORMAT_R8G8B8A8:
  7343. case NK_FORMAT_R8G8B8: {
  7344. struct nk_color col = nk_rgba_fv(values);
  7345. NK_MEMCPY(attribute, &col.r, sizeof(col));
  7346. } break;
  7347. case NK_FORMAT_B8G8R8A8: {
  7348. struct nk_color col = nk_rgba_fv(values);
  7349. struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
  7350. NK_MEMCPY(attribute, &bgra, sizeof(bgra));
  7351. } break;
  7352. case NK_FORMAT_R16G15B16: {
  7353. nk_ushort col[3];
  7354. col[0] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[0] * NK_USHORT_MAX, NK_USHORT_MAX);
  7355. col[1] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[1] * NK_USHORT_MAX, NK_USHORT_MAX);
  7356. col[2] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[2] * NK_USHORT_MAX, NK_USHORT_MAX);
  7357. NK_MEMCPY(attribute, col, sizeof(col));
  7358. } break;
  7359. case NK_FORMAT_R16G15B16A16: {
  7360. nk_ushort col[4];
  7361. col[0] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[0] * NK_USHORT_MAX, NK_USHORT_MAX);
  7362. col[1] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[1] * NK_USHORT_MAX, NK_USHORT_MAX);
  7363. col[2] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[2] * NK_USHORT_MAX, NK_USHORT_MAX);
  7364. col[3] = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[3] * NK_USHORT_MAX, NK_USHORT_MAX);
  7365. NK_MEMCPY(attribute, col, sizeof(col));
  7366. } break;
  7367. case NK_FORMAT_R32G32B32: {
  7368. nk_uint col[3];
  7369. col[0] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[0] * NK_UINT_MAX, NK_UINT_MAX);
  7370. col[1] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[1] * NK_UINT_MAX, NK_UINT_MAX);
  7371. col[2] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[2] * NK_UINT_MAX, NK_UINT_MAX);
  7372. NK_MEMCPY(attribute, col, sizeof(col));
  7373. } break;
  7374. case NK_FORMAT_R32G32B32A32: {
  7375. nk_uint col[4];
  7376. col[0] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[0] * NK_UINT_MAX, NK_UINT_MAX);
  7377. col[1] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[1] * NK_UINT_MAX, NK_UINT_MAX);
  7378. col[2] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[2] * NK_UINT_MAX, NK_UINT_MAX);
  7379. col[3] = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[3] * NK_UINT_MAX, NK_UINT_MAX);
  7380. NK_MEMCPY(attribute, col, sizeof(col));
  7381. } break;
  7382. case NK_FORMAT_R32G32B32A32_FLOAT:
  7383. NK_MEMCPY(attribute, values, sizeof(float)*4);
  7384. break;
  7385. case NK_FORMAT_R32G32B32A32_DOUBLE: {
  7386. double col[4];
  7387. col[0] = (double)NK_SATURATE(values[0]);
  7388. col[1] = (double)NK_SATURATE(values[1]);
  7389. col[2] = (double)NK_SATURATE(values[2]);
  7390. col[3] = (double)NK_SATURATE(values[3]);
  7391. NK_MEMCPY(attribute, col, sizeof(col));
  7392. } break;
  7393. case NK_FORMAT_RGB32:
  7394. case NK_FORMAT_RGBA32: {
  7395. struct nk_color col = nk_rgba_fv(values);
  7396. nk_uint color = nk_color_u32(col);
  7397. NK_MEMCPY(attribute, &color, sizeof(color));
  7398. } break;
  7399. }
  7400. }
  7401. NK_INTERN void
  7402. nk_draw_vertex_element(void *dst, const float *values, int value_count,
  7403. enum nk_draw_vertex_layout_format format)
  7404. {
  7405. int value_index;
  7406. void *attribute = dst;
  7407. /* if this triggers you tried to provide a color format for a value */
  7408. NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
  7409. if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
  7410. for (value_index = 0; value_index < value_count; ++value_index) {
  7411. switch (format) {
  7412. default: NK_ASSERT(0 && "invalid vertex layout format"); break;
  7413. case NK_FORMAT_SCHAR: {
  7414. char value = (char)NK_CLAMP(NK_SCHAR_MIN, values[value_index], NK_SCHAR_MAX);
  7415. NK_MEMCPY(attribute, &value, sizeof(value));
  7416. attribute = (void*)((char*)attribute + sizeof(char));
  7417. } break;
  7418. case NK_FORMAT_SSHORT: {
  7419. nk_short value = (nk_short)NK_CLAMP(NK_SSHORT_MIN, values[value_index], NK_SSHORT_MAX);
  7420. NK_MEMCPY(attribute, &value, sizeof(value));
  7421. attribute = (void*)((char*)attribute + sizeof(value));
  7422. } break;
  7423. case NK_FORMAT_SINT: {
  7424. nk_int value = (nk_int)NK_CLAMP(NK_SINT_MIN, values[value_index], NK_SINT_MAX);
  7425. NK_MEMCPY(attribute, &value, sizeof(value));
  7426. attribute = (void*)((char*)attribute + sizeof(nk_int));
  7427. } break;
  7428. case NK_FORMAT_UCHAR: {
  7429. unsigned char value = (unsigned char)NK_CLAMP(NK_UCHAR_MIN, values[value_index], NK_UCHAR_MAX);
  7430. NK_MEMCPY(attribute, &value, sizeof(value));
  7431. attribute = (void*)((char*)attribute + sizeof(unsigned char));
  7432. } break;
  7433. case NK_FORMAT_USHORT: {
  7434. nk_ushort value = (nk_ushort)NK_CLAMP(NK_USHORT_MIN, values[value_index], NK_USHORT_MAX);
  7435. NK_MEMCPY(attribute, &value, sizeof(value));
  7436. attribute = (void*)((char*)attribute + sizeof(value));
  7437. } break;
  7438. case NK_FORMAT_UINT: {
  7439. nk_uint value = (nk_uint)NK_CLAMP(NK_UINT_MIN, values[value_index], NK_UINT_MAX);
  7440. NK_MEMCPY(attribute, &value, sizeof(value));
  7441. attribute = (void*)((char*)attribute + sizeof(nk_uint));
  7442. } break;
  7443. case NK_FORMAT_FLOAT:
  7444. NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
  7445. attribute = (void*)((char*)attribute + sizeof(float));
  7446. break;
  7447. case NK_FORMAT_DOUBLE: {
  7448. double value = (double)values[value_index];
  7449. NK_MEMCPY(attribute, &value, sizeof(value));
  7450. attribute = (void*)((char*)attribute + sizeof(double));
  7451. } break;
  7452. }
  7453. }
  7454. }
  7455. NK_INTERN void*
  7456. nk_draw_vertex(void *dst, const struct nk_convert_config *config,
  7457. struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
  7458. {
  7459. void *result = (void*)((char*)dst + config->vertex_size);
  7460. const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
  7461. while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
  7462. void *address = (void*)((char*)dst + elem_iter->offset);
  7463. switch (elem_iter->attribute) {
  7464. case NK_VERTEX_ATTRIBUTE_COUNT:
  7465. default: NK_ASSERT(0 && "wrong element attribute");
  7466. case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
  7467. case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
  7468. case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
  7469. }
  7470. elem_iter++;
  7471. }
  7472. return result;
  7473. }
  7474. NK_API void
  7475. nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
  7476. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  7477. float thickness, enum nk_anti_aliasing aliasing)
  7478. {
  7479. nk_size count;
  7480. int thick_line;
  7481. struct nk_colorf col;
  7482. struct nk_colorf col_trans;
  7483. NK_ASSERT(list);
  7484. if (!list || points_count < 2) return;
  7485. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  7486. count = points_count;
  7487. if (!closed) count = points_count-1;
  7488. thick_line = thickness > 1.0f;
  7489. #ifdef NK_INCLUDE_COMMAND_USERDATA
  7490. nk_draw_list_push_userdata(list, list->userdata);
  7491. #endif
  7492. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  7493. nk_color_fv(&col.r, color);
  7494. col_trans = col;
  7495. col_trans.a = 0;
  7496. if (aliasing == NK_ANTI_ALIASING_ON) {
  7497. /* ANTI-ALIASED STROKE */
  7498. const float AA_SIZE = 1.0f;
  7499. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  7500. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  7501. /* allocate vertices and elements */
  7502. nk_size i1 = 0;
  7503. nk_size vertex_offset;
  7504. nk_size index = list->vertex_count;
  7505. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  7506. const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
  7507. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  7508. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  7509. nk_size size;
  7510. struct nk_vec2 *normals, *temp;
  7511. if (!vtx || !ids) return;
  7512. /* temporary allocate normals + points */
  7513. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  7514. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  7515. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  7516. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  7517. NK_ASSERT(normals);
  7518. if (!normals) return;
  7519. temp = normals + points_count;
  7520. /* make sure vertex pointer is still correct */
  7521. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  7522. /* calculate normals */
  7523. for (i1 = 0; i1 < count; ++i1) {
  7524. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  7525. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  7526. float len;
  7527. /* vec2 inverted length */
  7528. len = nk_vec2_len_sqr(diff);
  7529. if (len != 0.0f)
  7530. len = nk_inv_sqrt(len);
  7531. else len = 1.0f;
  7532. diff = nk_vec2_muls(diff, len);
  7533. normals[i1].x = diff.y;
  7534. normals[i1].y = -diff.x;
  7535. }
  7536. if (!closed)
  7537. normals[points_count-1] = normals[points_count-2];
  7538. if (!thick_line) {
  7539. nk_size idx1, i;
  7540. if (!closed) {
  7541. struct nk_vec2 d;
  7542. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  7543. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  7544. d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
  7545. temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
  7546. temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
  7547. }
  7548. /* fill elements */
  7549. idx1 = index;
  7550. for (i1 = 0; i1 < count; i1++) {
  7551. struct nk_vec2 dm;
  7552. float dmr2;
  7553. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  7554. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
  7555. /* average normals */
  7556. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  7557. dmr2 = dm.x * dm.x + dm.y* dm.y;
  7558. if (dmr2 > 0.000001f) {
  7559. float scale = 1.0f/dmr2;
  7560. scale = NK_MIN(100.0f, scale);
  7561. dm = nk_vec2_muls(dm, scale);
  7562. }
  7563. dm = nk_vec2_muls(dm, AA_SIZE);
  7564. temp[i2*2+0] = nk_vec2_add(points[i2], dm);
  7565. temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
  7566. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
  7567. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  7568. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
  7569. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  7570. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  7571. ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
  7572. ids += 12;
  7573. idx1 = idx2;
  7574. }
  7575. /* fill vertices */
  7576. for (i = 0; i < points_count; ++i) {
  7577. const struct nk_vec2 uv = list->config.null.uv;
  7578. vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
  7579. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
  7580. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
  7581. }
  7582. } else {
  7583. nk_size idx1, i;
  7584. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  7585. if (!closed) {
  7586. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  7587. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  7588. temp[0] = nk_vec2_add(points[0], d1);
  7589. temp[1] = nk_vec2_add(points[0], d2);
  7590. temp[2] = nk_vec2_sub(points[0], d2);
  7591. temp[3] = nk_vec2_sub(points[0], d1);
  7592. d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
  7593. d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
  7594. temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
  7595. temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
  7596. temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
  7597. temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
  7598. }
  7599. /* add all elements */
  7600. idx1 = index;
  7601. for (i1 = 0; i1 < count; ++i1) {
  7602. struct nk_vec2 dm_out, dm_in;
  7603. const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
  7604. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
  7605. /* average normals */
  7606. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  7607. float dmr2 = dm.x * dm.x + dm.y* dm.y;
  7608. if (dmr2 > 0.000001f) {
  7609. float scale = 1.0f/dmr2;
  7610. scale = NK_MIN(100.0f, scale);
  7611. dm = nk_vec2_muls(dm, scale);
  7612. }
  7613. dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
  7614. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  7615. temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
  7616. temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
  7617. temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
  7618. temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
  7619. /* add indexes */
  7620. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
  7621. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  7622. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
  7623. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  7624. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  7625. ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
  7626. ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
  7627. ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
  7628. ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
  7629. ids += 18;
  7630. idx1 = idx2;
  7631. }
  7632. /* add vertices */
  7633. for (i = 0; i < points_count; ++i) {
  7634. const struct nk_vec2 uv = list->config.null.uv;
  7635. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
  7636. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
  7637. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
  7638. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
  7639. }
  7640. }
  7641. /* free temporary normals + points */
  7642. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  7643. } else {
  7644. /* NON ANTI-ALIASED STROKE */
  7645. nk_size i1 = 0;
  7646. nk_size idx = list->vertex_count;
  7647. const nk_size idx_count = count * 6;
  7648. const nk_size vtx_count = count * 4;
  7649. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  7650. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  7651. if (!vtx || !ids) return;
  7652. for (i1 = 0; i1 < count; ++i1) {
  7653. float dx, dy;
  7654. const struct nk_vec2 uv = list->config.null.uv;
  7655. const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
  7656. const struct nk_vec2 p1 = points[i1];
  7657. const struct nk_vec2 p2 = points[i2];
  7658. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  7659. float len;
  7660. /* vec2 inverted length */
  7661. len = nk_vec2_len_sqr(diff);
  7662. if (len != 0.0f)
  7663. len = nk_inv_sqrt(len);
  7664. else len = 1.0f;
  7665. diff = nk_vec2_muls(diff, len);
  7666. /* add vertices */
  7667. dx = diff.x * (thickness * 0.5f);
  7668. dy = diff.y * (thickness * 0.5f);
  7669. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  7670. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  7671. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  7672. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  7673. ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
  7674. ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
  7675. ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
  7676. ids += 6;
  7677. idx += 4;
  7678. }
  7679. }
  7680. }
  7681. NK_API void
  7682. nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
  7683. const struct nk_vec2 *points, const unsigned int points_count,
  7684. struct nk_color color, enum nk_anti_aliasing aliasing)
  7685. {
  7686. struct nk_colorf col;
  7687. struct nk_colorf col_trans;
  7688. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  7689. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  7690. NK_ASSERT(list);
  7691. if (!list || points_count < 3) return;
  7692. #ifdef NK_INCLUDE_COMMAND_USERDATA
  7693. nk_draw_list_push_userdata(list, list->userdata);
  7694. #endif
  7695. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  7696. nk_color_fv(&col.r, color);
  7697. col_trans = col;
  7698. col_trans.a = 0;
  7699. if (aliasing == NK_ANTI_ALIASING_ON) {
  7700. nk_size i = 0;
  7701. nk_size i0 = 0;
  7702. nk_size i1 = 0;
  7703. const float AA_SIZE = 1.0f;
  7704. nk_size vertex_offset = 0;
  7705. nk_size index = list->vertex_count;
  7706. const nk_size idx_count = (points_count-2)*3 + points_count*6;
  7707. const nk_size vtx_count = (points_count*2);
  7708. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  7709. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  7710. nk_size size = 0;
  7711. struct nk_vec2 *normals = 0;
  7712. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  7713. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  7714. if (!vtx || !ids) return;
  7715. /* temporary allocate normals */
  7716. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  7717. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  7718. size = pnt_size * points_count;
  7719. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  7720. NK_ASSERT(normals);
  7721. if (!normals) return;
  7722. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  7723. /* add elements */
  7724. for (i = 2; i < points_count; i++) {
  7725. ids[0] = (nk_draw_index)(vtx_inner_idx);
  7726. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
  7727. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  7728. ids += 3;
  7729. }
  7730. /* compute normals */
  7731. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  7732. struct nk_vec2 p0 = points[i0];
  7733. struct nk_vec2 p1 = points[i1];
  7734. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  7735. /* vec2 inverted length */
  7736. float len = nk_vec2_len_sqr(diff);
  7737. if (len != 0.0f)
  7738. len = nk_inv_sqrt(len);
  7739. else len = 1.0f;
  7740. diff = nk_vec2_muls(diff, len);
  7741. normals[i0].x = diff.y;
  7742. normals[i0].y = -diff.x;
  7743. }
  7744. /* add vertices + indexes */
  7745. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  7746. const struct nk_vec2 uv = list->config.null.uv;
  7747. struct nk_vec2 n0 = normals[i0];
  7748. struct nk_vec2 n1 = normals[i1];
  7749. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  7750. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  7751. if (dmr2 > 0.000001f) {
  7752. float scale = 1.0f / dmr2;
  7753. scale = NK_MIN(scale, 100.0f);
  7754. dm = nk_vec2_muls(dm, scale);
  7755. }
  7756. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  7757. /* add vertices */
  7758. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
  7759. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
  7760. /* add indexes */
  7761. ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  7762. ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
  7763. ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  7764. ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  7765. ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
  7766. ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  7767. ids += 6;
  7768. }
  7769. /* free temporary normals + points */
  7770. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  7771. } else {
  7772. nk_size i = 0;
  7773. nk_size index = list->vertex_count;
  7774. const nk_size idx_count = (points_count-2)*3;
  7775. const nk_size vtx_count = points_count;
  7776. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  7777. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  7778. if (!vtx || !ids) return;
  7779. for (i = 0; i < vtx_count; ++i)
  7780. vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
  7781. for (i = 2; i < points_count; ++i) {
  7782. ids[0] = (nk_draw_index)index;
  7783. ids[1] = (nk_draw_index)(index+ i - 1);
  7784. ids[2] = (nk_draw_index)(index+i);
  7785. ids += 3;
  7786. }
  7787. }
  7788. }
  7789. NK_API void
  7790. nk_draw_list_path_clear(struct nk_draw_list *list)
  7791. {
  7792. NK_ASSERT(list);
  7793. if (!list) return;
  7794. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  7795. list->path_count = 0;
  7796. list->path_offset = 0;
  7797. }
  7798. NK_API void
  7799. nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
  7800. {
  7801. struct nk_vec2 *points = 0;
  7802. struct nk_draw_command *cmd = 0;
  7803. NK_ASSERT(list);
  7804. if (!list) return;
  7805. if (!list->cmd_count)
  7806. nk_draw_list_add_clip(list, nk_null_rect);
  7807. cmd = nk_draw_list_command_last(list);
  7808. if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
  7809. nk_draw_list_push_image(list, list->config.null.texture);
  7810. points = nk_draw_list_alloc_path(list, 1);
  7811. if (!points) return;
  7812. points[0] = pos;
  7813. }
  7814. NK_API void
  7815. nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
  7816. float radius, int a_min, int a_max)
  7817. {
  7818. int a = 0;
  7819. NK_ASSERT(list);
  7820. if (!list) return;
  7821. if (a_min <= a_max) {
  7822. for (a = a_min; a <= a_max; a++) {
  7823. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  7824. const float x = center.x + c.x * radius;
  7825. const float y = center.y + c.y * radius;
  7826. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  7827. }
  7828. }
  7829. }
  7830. NK_API void
  7831. nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
  7832. float radius, float a_min, float a_max, unsigned int segments)
  7833. {
  7834. unsigned int i = 0;
  7835. NK_ASSERT(list);
  7836. if (!list) return;
  7837. if (radius == 0.0f) return;
  7838. for (i = 0; i <= segments; ++i) {
  7839. const float a = a_min + ((float)i / ((float)segments) * (a_max - a_min));
  7840. const float x = center.x + (float)NK_COS(a) * radius;
  7841. const float y = center.y + (float)NK_SIN(a) * radius;
  7842. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  7843. }
  7844. }
  7845. NK_API void
  7846. nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
  7847. struct nk_vec2 b, float rounding)
  7848. {
  7849. float r;
  7850. NK_ASSERT(list);
  7851. if (!list) return;
  7852. r = rounding;
  7853. r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
  7854. r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
  7855. if (r == 0.0f) {
  7856. nk_draw_list_path_line_to(list, a);
  7857. nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
  7858. nk_draw_list_path_line_to(list, b);
  7859. nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
  7860. } else {
  7861. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  7862. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  7863. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  7864. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  7865. }
  7866. }
  7867. NK_API void
  7868. nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
  7869. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  7870. {
  7871. float t_step;
  7872. unsigned int i_step;
  7873. struct nk_vec2 p1;
  7874. NK_ASSERT(list);
  7875. NK_ASSERT(list->path_count);
  7876. if (!list || !list->path_count) return;
  7877. num_segments = NK_MAX(num_segments, 1);
  7878. p1 = nk_draw_list_path_last(list);
  7879. t_step = 1.0f/(float)num_segments;
  7880. for (i_step = 1; i_step <= num_segments; ++i_step) {
  7881. float t = t_step * (float)i_step;
  7882. float u = 1.0f - t;
  7883. float w1 = u*u*u;
  7884. float w2 = 3*u*u*t;
  7885. float w3 = 3*u*t*t;
  7886. float w4 = t * t *t;
  7887. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  7888. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  7889. nk_draw_list_path_line_to(list, nk_vec2(x,y));
  7890. }
  7891. }
  7892. NK_API void
  7893. nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
  7894. {
  7895. struct nk_vec2 *points;
  7896. NK_ASSERT(list);
  7897. if (!list) return;
  7898. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  7899. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
  7900. nk_draw_list_path_clear(list);
  7901. }
  7902. NK_API void
  7903. nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
  7904. enum nk_draw_list_stroke closed, float thickness)
  7905. {
  7906. struct nk_vec2 *points;
  7907. NK_ASSERT(list);
  7908. if (!list) return;
  7909. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  7910. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  7911. closed, thickness, list->config.line_AA);
  7912. nk_draw_list_path_clear(list);
  7913. }
  7914. NK_API void
  7915. nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
  7916. struct nk_vec2 b, struct nk_color col, float thickness)
  7917. {
  7918. NK_ASSERT(list);
  7919. if (!list || !col.a) return;
  7920. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  7921. nk_draw_list_path_line_to(list, a);
  7922. nk_draw_list_path_line_to(list, b);
  7923. } else {
  7924. nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
  7925. nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
  7926. }
  7927. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  7928. }
  7929. NK_API void
  7930. nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
  7931. struct nk_color col, float rounding)
  7932. {
  7933. NK_ASSERT(list);
  7934. if (!list || !col.a) return;
  7935. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  7936. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  7937. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  7938. } else {
  7939. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  7940. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  7941. } nk_draw_list_path_fill(list, col);
  7942. }
  7943. NK_API void
  7944. nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
  7945. struct nk_color col, float rounding, float thickness)
  7946. {
  7947. NK_ASSERT(list);
  7948. if (!list || !col.a) return;
  7949. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  7950. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  7951. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  7952. } else {
  7953. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  7954. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  7955. } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  7956. }
  7957. NK_API void
  7958. nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
  7959. struct nk_color left, struct nk_color top, struct nk_color right,
  7960. struct nk_color bottom)
  7961. {
  7962. void *vtx;
  7963. struct nk_colorf col_left, col_top;
  7964. struct nk_colorf col_right, col_bottom;
  7965. nk_draw_index *idx;
  7966. nk_draw_index index;
  7967. nk_color_fv(&col_left.r, left);
  7968. nk_color_fv(&col_right.r, right);
  7969. nk_color_fv(&col_top.r, top);
  7970. nk_color_fv(&col_bottom.r, bottom);
  7971. NK_ASSERT(list);
  7972. if (!list) return;
  7973. nk_draw_list_push_image(list, list->config.null.texture);
  7974. index = (nk_draw_index)list->vertex_count;
  7975. vtx = nk_draw_list_alloc_vertices(list, 4);
  7976. idx = nk_draw_list_alloc_elements(list, 6);
  7977. if (!vtx || !idx) return;
  7978. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  7979. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  7980. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  7981. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
  7982. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
  7983. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
  7984. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
  7985. }
  7986. NK_API void
  7987. nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  7988. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  7989. {
  7990. NK_ASSERT(list);
  7991. if (!list || !col.a) return;
  7992. nk_draw_list_path_line_to(list, a);
  7993. nk_draw_list_path_line_to(list, b);
  7994. nk_draw_list_path_line_to(list, c);
  7995. nk_draw_list_path_fill(list, col);
  7996. }
  7997. NK_API void
  7998. nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  7999. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  8000. {
  8001. NK_ASSERT(list);
  8002. if (!list || !col.a) return;
  8003. nk_draw_list_path_line_to(list, a);
  8004. nk_draw_list_path_line_to(list, b);
  8005. nk_draw_list_path_line_to(list, c);
  8006. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  8007. }
  8008. NK_API void
  8009. nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
  8010. float radius, struct nk_color col, unsigned int segs)
  8011. {
  8012. float a_max;
  8013. NK_ASSERT(list);
  8014. if (!list || !col.a) return;
  8015. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  8016. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  8017. nk_draw_list_path_fill(list, col);
  8018. }
  8019. NK_API void
  8020. nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
  8021. float radius, struct nk_color col, unsigned int segs, float thickness)
  8022. {
  8023. float a_max;
  8024. NK_ASSERT(list);
  8025. if (!list || !col.a) return;
  8026. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  8027. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  8028. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  8029. }
  8030. NK_API void
  8031. nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
  8032. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  8033. struct nk_color col, unsigned int segments, float thickness)
  8034. {
  8035. NK_ASSERT(list);
  8036. if (!list || !col.a) return;
  8037. nk_draw_list_path_line_to(list, p0);
  8038. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  8039. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  8040. }
  8041. NK_INTERN void
  8042. nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
  8043. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  8044. struct nk_color color)
  8045. {
  8046. void *vtx;
  8047. struct nk_vec2 uvb;
  8048. struct nk_vec2 uvd;
  8049. struct nk_vec2 b;
  8050. struct nk_vec2 d;
  8051. struct nk_colorf col;
  8052. nk_draw_index *idx;
  8053. nk_draw_index index;
  8054. NK_ASSERT(list);
  8055. if (!list) return;
  8056. nk_color_fv(&col.r, color);
  8057. uvb = nk_vec2(uvc.x, uva.y);
  8058. uvd = nk_vec2(uva.x, uvc.y);
  8059. b = nk_vec2(c.x, a.y);
  8060. d = nk_vec2(a.x, c.y);
  8061. index = (nk_draw_index)list->vertex_count;
  8062. vtx = nk_draw_list_alloc_vertices(list, 4);
  8063. idx = nk_draw_list_alloc_elements(list, 6);
  8064. if (!vtx || !idx) return;
  8065. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  8066. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  8067. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  8068. vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
  8069. vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
  8070. vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
  8071. vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
  8072. }
  8073. NK_API void
  8074. nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
  8075. struct nk_rect rect, struct nk_color color)
  8076. {
  8077. NK_ASSERT(list);
  8078. if (!list) return;
  8079. /* push new command with given texture */
  8080. nk_draw_list_push_image(list, texture.handle);
  8081. if (nk_image_is_subimage(&texture)) {
  8082. /* add region inside of the texture */
  8083. struct nk_vec2 uv[2];
  8084. uv[0].x = (float)texture.region[0]/(float)texture.w;
  8085. uv[0].y = (float)texture.region[1]/(float)texture.h;
  8086. uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
  8087. uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
  8088. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  8089. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  8090. } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  8091. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  8092. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
  8093. }
  8094. NK_API void
  8095. nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
  8096. struct nk_rect rect, const char *text, int len, float font_height,
  8097. struct nk_color fg)
  8098. {
  8099. float x = 0;
  8100. int text_len = 0;
  8101. nk_rune unicode = 0;
  8102. nk_rune next = 0;
  8103. int glyph_len = 0;
  8104. int next_glyph_len = 0;
  8105. struct nk_user_font_glyph g;
  8106. NK_ASSERT(list);
  8107. if (!list || !len || !text) return;
  8108. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8109. list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
  8110. nk_draw_list_push_image(list, font->texture);
  8111. x = rect.x;
  8112. glyph_len = nk_utf_decode(text, &unicode, len);
  8113. if (!glyph_len) return;
  8114. /* draw every glyph image */
  8115. fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
  8116. while (text_len < len && glyph_len) {
  8117. float gx, gy, gh, gw;
  8118. float char_width = 0;
  8119. if (unicode == NK_UTF_INVALID) break;
  8120. /* query currently drawn glyph information */
  8121. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  8122. font->query(font->userdata, font_height, &g, unicode,
  8123. (next == NK_UTF_INVALID) ? '\0' : next);
  8124. /* calculate and draw glyph drawing rectangle and image */
  8125. gx = x + g.offset.x;
  8126. gy = rect.y + g.offset.y;
  8127. gw = g.width; gh = g.height;
  8128. char_width = g.xadvance;
  8129. nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
  8130. g.uv[0], g.uv[1], fg);
  8131. /* offset next glyph */
  8132. text_len += glyph_len;
  8133. x += char_width;
  8134. glyph_len = next_glyph_len;
  8135. unicode = next;
  8136. }
  8137. }
  8138. NK_API nk_flags
  8139. nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  8140. struct nk_buffer *vertices, struct nk_buffer *elements,
  8141. const struct nk_convert_config *config)
  8142. {
  8143. nk_flags res = NK_CONVERT_SUCCESS;
  8144. const struct nk_command *cmd;
  8145. NK_ASSERT(ctx);
  8146. NK_ASSERT(cmds);
  8147. NK_ASSERT(vertices);
  8148. NK_ASSERT(elements);
  8149. NK_ASSERT(config);
  8150. NK_ASSERT(config->vertex_layout);
  8151. NK_ASSERT(config->vertex_size);
  8152. if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
  8153. return NK_CONVERT_INVALID_PARAM;
  8154. nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
  8155. config->line_AA, config->shape_AA);
  8156. nk_foreach(cmd, ctx)
  8157. {
  8158. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8159. ctx->draw_list.userdata = cmd->userdata;
  8160. #endif
  8161. switch (cmd->type) {
  8162. case NK_COMMAND_NOP: break;
  8163. case NK_COMMAND_SCISSOR: {
  8164. const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
  8165. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  8166. } break;
  8167. case NK_COMMAND_LINE: {
  8168. const struct nk_command_line *l = (const struct nk_command_line*)cmd;
  8169. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  8170. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  8171. } break;
  8172. case NK_COMMAND_CURVE: {
  8173. const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
  8174. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  8175. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  8176. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  8177. config->curve_segment_count, q->line_thickness);
  8178. } break;
  8179. case NK_COMMAND_RECT: {
  8180. const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
  8181. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  8182. r->color, (float)r->rounding, r->line_thickness);
  8183. } break;
  8184. case NK_COMMAND_RECT_FILLED: {
  8185. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
  8186. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  8187. r->color, (float)r->rounding);
  8188. } break;
  8189. case NK_COMMAND_RECT_MULTI_COLOR: {
  8190. const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
  8191. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  8192. r->left, r->top, r->right, r->bottom);
  8193. } break;
  8194. case NK_COMMAND_CIRCLE: {
  8195. const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
  8196. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  8197. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  8198. config->circle_segment_count, c->line_thickness);
  8199. } break;
  8200. case NK_COMMAND_CIRCLE_FILLED: {
  8201. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  8202. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  8203. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  8204. config->circle_segment_count);
  8205. } break;
  8206. case NK_COMMAND_ARC: {
  8207. const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
  8208. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  8209. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  8210. c->a[0], c->a[1], config->arc_segment_count);
  8211. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  8212. } break;
  8213. case NK_COMMAND_ARC_FILLED: {
  8214. const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
  8215. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  8216. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  8217. c->a[0], c->a[1], config->arc_segment_count);
  8218. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  8219. } break;
  8220. case NK_COMMAND_TRIANGLE: {
  8221. const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
  8222. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  8223. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  8224. t->line_thickness);
  8225. } break;
  8226. case NK_COMMAND_TRIANGLE_FILLED: {
  8227. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
  8228. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  8229. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  8230. } break;
  8231. case NK_COMMAND_POLYGON: {
  8232. int i;
  8233. const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
  8234. for (i = 0; i < p->point_count; ++i) {
  8235. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  8236. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  8237. }
  8238. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  8239. } break;
  8240. case NK_COMMAND_POLYGON_FILLED: {
  8241. int i;
  8242. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
  8243. for (i = 0; i < p->point_count; ++i) {
  8244. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  8245. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  8246. }
  8247. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  8248. } break;
  8249. case NK_COMMAND_POLYLINE: {
  8250. int i;
  8251. const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
  8252. for (i = 0; i < p->point_count; ++i) {
  8253. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  8254. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  8255. }
  8256. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  8257. } break;
  8258. case NK_COMMAND_TEXT: {
  8259. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  8260. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  8261. t->string, t->length, t->height, t->foreground);
  8262. } break;
  8263. case NK_COMMAND_IMAGE: {
  8264. const struct nk_command_image *i = (const struct nk_command_image*)cmd;
  8265. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
  8266. } break;
  8267. case NK_COMMAND_CUSTOM: {
  8268. const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
  8269. c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
  8270. } break;
  8271. default: break;
  8272. }
  8273. }
  8274. res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
  8275. res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
  8276. res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
  8277. return res;
  8278. }
  8279. NK_API const struct nk_draw_command*
  8280. nk__draw_begin(const struct nk_context *ctx,
  8281. const struct nk_buffer *buffer)
  8282. {return nk__draw_list_begin(&ctx->draw_list, buffer);}
  8283. NK_API const struct nk_draw_command*
  8284. nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
  8285. {return nk__draw_list_end(&ctx->draw_list, buffer);}
  8286. NK_API const struct nk_draw_command*
  8287. nk__draw_next(const struct nk_draw_command *cmd,
  8288. const struct nk_buffer *buffer, const struct nk_context *ctx)
  8289. {return nk__draw_list_next(cmd, buffer, &ctx->draw_list);}
  8290. #endif
  8291. /*
  8292. * ==============================================================
  8293. *
  8294. * FONT HANDLING
  8295. *
  8296. * ===============================================================
  8297. */
  8298. #ifdef NK_INCLUDE_FONT_BAKING
  8299. /* -------------------------------------------------------------
  8300. *
  8301. * RECT PACK
  8302. *
  8303. * --------------------------------------------------------------*/
  8304. /* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
  8305. /* Sean Barrett 2014 */
  8306. #define NK_RP__MAXVAL 0xffff
  8307. typedef unsigned short nk_rp_coord;
  8308. struct nk_rp_rect {
  8309. /* reserved for your use: */
  8310. int id;
  8311. /* input: */
  8312. nk_rp_coord w, h;
  8313. /* output: */
  8314. nk_rp_coord x, y;
  8315. int was_packed;
  8316. /* non-zero if valid packing */
  8317. }; /* 16 bytes, nominally */
  8318. struct nk_rp_node {
  8319. nk_rp_coord x,y;
  8320. struct nk_rp_node *next;
  8321. };
  8322. struct nk_rp_context {
  8323. int width;
  8324. int height;
  8325. int align;
  8326. int init_mode;
  8327. int heuristic;
  8328. int num_nodes;
  8329. struct nk_rp_node *active_head;
  8330. struct nk_rp_node *free_head;
  8331. struct nk_rp_node extra[2];
  8332. /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  8333. };
  8334. struct nk_rp__findresult {
  8335. int x,y;
  8336. struct nk_rp_node **prev_link;
  8337. };
  8338. enum NK_RP_HEURISTIC {
  8339. NK_RP_HEURISTIC_Skyline_default=0,
  8340. NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
  8341. NK_RP_HEURISTIC_Skyline_BF_sortHeight
  8342. };
  8343. enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
  8344. NK_INTERN void
  8345. nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
  8346. {
  8347. if (allow_out_of_mem)
  8348. /* if it's ok to run out of memory, then don't bother aligning them; */
  8349. /* this gives better packing, but may fail due to OOM (even though */
  8350. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  8351. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  8352. context->align = 1;
  8353. else {
  8354. /* if it's not ok to run out of memory, then quantize the widths */
  8355. /* so that num_nodes is always enough nodes. */
  8356. /* */
  8357. /* I.e. num_nodes * align >= width */
  8358. /* align >= width / num_nodes */
  8359. /* align = ceil(width/num_nodes) */
  8360. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  8361. }
  8362. }
  8363. NK_INTERN void
  8364. nk_rp_init_target(struct nk_rp_context *context, int width, int height,
  8365. struct nk_rp_node *nodes, int num_nodes)
  8366. {
  8367. int i;
  8368. #ifndef STBRP_LARGE_RECTS
  8369. NK_ASSERT(width <= 0xffff && height <= 0xffff);
  8370. #endif
  8371. for (i=0; i < num_nodes-1; ++i)
  8372. nodes[i].next = &nodes[i+1];
  8373. nodes[i].next = 0;
  8374. context->init_mode = NK_RP__INIT_skyline;
  8375. context->heuristic = NK_RP_HEURISTIC_Skyline_default;
  8376. context->free_head = &nodes[0];
  8377. context->active_head = &context->extra[0];
  8378. context->width = width;
  8379. context->height = height;
  8380. context->num_nodes = num_nodes;
  8381. nk_rp_setup_allow_out_of_mem(context, 0);
  8382. /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
  8383. context->extra[0].x = 0;
  8384. context->extra[0].y = 0;
  8385. context->extra[0].next = &context->extra[1];
  8386. context->extra[1].x = (nk_rp_coord) width;
  8387. context->extra[1].y = 65535;
  8388. context->extra[1].next = 0;
  8389. }
  8390. /* find minimum y position if it starts at x1 */
  8391. NK_INTERN int
  8392. nk_rp__skyline_find_min_y(struct nk_rp_context *c, struct nk_rp_node *first,
  8393. int x0, int width, int *pwaste)
  8394. {
  8395. struct nk_rp_node *node = first;
  8396. int x1 = x0 + width;
  8397. int min_y, visited_width, waste_area;
  8398. NK_ASSERT(first->x <= x0);
  8399. NK_UNUSED(c);
  8400. NK_ASSERT(node->next->x > x0);
  8401. /* we ended up handling this in the caller for efficiency */
  8402. NK_ASSERT(node->x <= x0);
  8403. min_y = 0;
  8404. waste_area = 0;
  8405. visited_width = 0;
  8406. while (node->x < x1)
  8407. {
  8408. if (node->y > min_y) {
  8409. /* raise min_y higher. */
  8410. /* we've accounted for all waste up to min_y, */
  8411. /* but we'll now add more waste for everything we've visited */
  8412. waste_area += visited_width * (node->y - min_y);
  8413. min_y = node->y;
  8414. /* the first time through, visited_width might be reduced */
  8415. if (node->x < x0)
  8416. visited_width += node->next->x - x0;
  8417. else
  8418. visited_width += node->next->x - node->x;
  8419. } else {
  8420. /* add waste area */
  8421. int under_width = node->next->x - node->x;
  8422. if (under_width + visited_width > width)
  8423. under_width = width - visited_width;
  8424. waste_area += under_width * (min_y - node->y);
  8425. visited_width += under_width;
  8426. }
  8427. node = node->next;
  8428. }
  8429. *pwaste = waste_area;
  8430. return min_y;
  8431. }
  8432. NK_INTERN struct nk_rp__findresult
  8433. nk_rp__skyline_find_best_pos(struct nk_rp_context *c, int width, int height)
  8434. {
  8435. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  8436. struct nk_rp__findresult fr;
  8437. struct nk_rp_node **prev, *node, *tail, **best = 0;
  8438. /* align to multiple of c->align */
  8439. width = (width + c->align - 1);
  8440. width -= width % c->align;
  8441. NK_ASSERT(width % c->align == 0);
  8442. node = c->active_head;
  8443. prev = &c->active_head;
  8444. while (node->x + width <= c->width) {
  8445. int y,waste;
  8446. y = nk_rp__skyline_find_min_y(c, node, node->x, width, &waste);
  8447. /* actually just want to test BL */
  8448. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BL_sortHeight) {
  8449. /* bottom left */
  8450. if (y < best_y) {
  8451. best_y = y;
  8452. best = prev;
  8453. }
  8454. } else {
  8455. /* best-fit */
  8456. if (y + height <= c->height) {
  8457. /* can only use it if it first vertically */
  8458. if (y < best_y || (y == best_y && waste < best_waste)) {
  8459. best_y = y;
  8460. best_waste = waste;
  8461. best = prev;
  8462. }
  8463. }
  8464. }
  8465. prev = &node->next;
  8466. node = node->next;
  8467. }
  8468. best_x = (best == 0) ? 0 : (*best)->x;
  8469. /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
  8470. /* */
  8471. /* e.g, if fitting */
  8472. /* */
  8473. /* ____________________ */
  8474. /* |____________________| */
  8475. /* */
  8476. /* into */
  8477. /* */
  8478. /* | | */
  8479. /* | ____________| */
  8480. /* |____________| */
  8481. /* */
  8482. /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
  8483. /* */
  8484. /* This makes BF take about 2x the time */
  8485. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BF_sortHeight)
  8486. {
  8487. tail = c->active_head;
  8488. node = c->active_head;
  8489. prev = &c->active_head;
  8490. /* find first node that's admissible */
  8491. while (tail->x < width)
  8492. tail = tail->next;
  8493. while (tail)
  8494. {
  8495. int xpos = tail->x - width;
  8496. int y,waste;
  8497. NK_ASSERT(xpos >= 0);
  8498. /* find the left position that matches this */
  8499. while (node->next->x <= xpos) {
  8500. prev = &node->next;
  8501. node = node->next;
  8502. }
  8503. NK_ASSERT(node->next->x > xpos && node->x <= xpos);
  8504. y = nk_rp__skyline_find_min_y(c, node, xpos, width, &waste);
  8505. if (y + height < c->height) {
  8506. if (y <= best_y) {
  8507. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  8508. best_x = xpos;
  8509. NK_ASSERT(y <= best_y);
  8510. best_y = y;
  8511. best_waste = waste;
  8512. best = prev;
  8513. }
  8514. }
  8515. }
  8516. tail = tail->next;
  8517. }
  8518. }
  8519. fr.prev_link = best;
  8520. fr.x = best_x;
  8521. fr.y = best_y;
  8522. return fr;
  8523. }
  8524. NK_INTERN struct nk_rp__findresult
  8525. nk_rp__skyline_pack_rectangle(struct nk_rp_context *context, int width, int height)
  8526. {
  8527. /* find best position according to heuristic */
  8528. struct nk_rp__findresult res = nk_rp__skyline_find_best_pos(context, width, height);
  8529. struct nk_rp_node *node, *cur;
  8530. /* bail if: */
  8531. /* 1. it failed */
  8532. /* 2. the best node doesn't fit (we don't always check this) */
  8533. /* 3. we're out of memory */
  8534. if (res.prev_link == 0 || res.y + height > context->height || context->free_head == 0) {
  8535. res.prev_link = 0;
  8536. return res;
  8537. }
  8538. /* on success, create new node */
  8539. node = context->free_head;
  8540. node->x = (nk_rp_coord) res.x;
  8541. node->y = (nk_rp_coord) (res.y + height);
  8542. context->free_head = node->next;
  8543. /* insert the new node into the right starting point, and */
  8544. /* let 'cur' point to the remaining nodes needing to be */
  8545. /* stitched back in */
  8546. cur = *res.prev_link;
  8547. if (cur->x < res.x) {
  8548. /* preserve the existing one, so start testing with the next one */
  8549. struct nk_rp_node *next = cur->next;
  8550. cur->next = node;
  8551. cur = next;
  8552. } else {
  8553. *res.prev_link = node;
  8554. }
  8555. /* from here, traverse cur and free the nodes, until we get to one */
  8556. /* that shouldn't be freed */
  8557. while (cur->next && cur->next->x <= res.x + width) {
  8558. struct nk_rp_node *next = cur->next;
  8559. /* move the current node to the free list */
  8560. cur->next = context->free_head;
  8561. context->free_head = cur;
  8562. cur = next;
  8563. }
  8564. /* stitch the list back in */
  8565. node->next = cur;
  8566. if (cur->x < res.x + width)
  8567. cur->x = (nk_rp_coord) (res.x + width);
  8568. return res;
  8569. }
  8570. NK_INTERN int
  8571. nk_rect_height_compare(const void *a, const void *b)
  8572. {
  8573. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  8574. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  8575. if (p->h > q->h)
  8576. return -1;
  8577. if (p->h < q->h)
  8578. return 1;
  8579. return (p->w > q->w) ? -1 : (p->w < q->w);
  8580. }
  8581. NK_INTERN int
  8582. nk_rect_original_order(const void *a, const void *b)
  8583. {
  8584. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  8585. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  8586. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  8587. }
  8588. NK_INTERN void
  8589. nk_rp_qsort(struct nk_rp_rect *array, unsigned int len, int(*cmp)(const void*,const void*))
  8590. {
  8591. /* iterative quick sort */
  8592. #define NK_MAX_SORT_STACK 64
  8593. unsigned right, left = 0, stack[NK_MAX_SORT_STACK], pos = 0;
  8594. unsigned seed = len/2 * 69069+1;
  8595. for (;;) {
  8596. for (; left+1 < len; len++) {
  8597. struct nk_rp_rect pivot, tmp;
  8598. if (pos == NK_MAX_SORT_STACK) len = stack[pos = 0];
  8599. pivot = array[left+seed%(len-left)];
  8600. seed = seed * 69069 + 1;
  8601. stack[pos++] = len;
  8602. for (right = left-1;;) {
  8603. while (cmp(&array[++right], &pivot) < 0);
  8604. while (cmp(&pivot, &array[--len]) < 0);
  8605. if (right >= len) break;
  8606. tmp = array[right];
  8607. array[right] = array[len];
  8608. array[len] = tmp;
  8609. }
  8610. }
  8611. if (pos == 0) break;
  8612. left = len;
  8613. len = stack[--pos];
  8614. }
  8615. #undef NK_MAX_SORT_STACK
  8616. }
  8617. NK_INTERN void
  8618. nk_rp_pack_rects(struct nk_rp_context *context, struct nk_rp_rect *rects, int num_rects)
  8619. {
  8620. int i;
  8621. /* we use the 'was_packed' field internally to allow sorting/unsorting */
  8622. for (i=0; i < num_rects; ++i) {
  8623. rects[i].was_packed = i;
  8624. }
  8625. /* sort according to heuristic */
  8626. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_height_compare);
  8627. for (i=0; i < num_rects; ++i) {
  8628. struct nk_rp__findresult fr = nk_rp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  8629. if (fr.prev_link) {
  8630. rects[i].x = (nk_rp_coord) fr.x;
  8631. rects[i].y = (nk_rp_coord) fr.y;
  8632. } else {
  8633. rects[i].x = rects[i].y = NK_RP__MAXVAL;
  8634. }
  8635. }
  8636. /* unsort */
  8637. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_original_order);
  8638. /* set was_packed flags */
  8639. for (i=0; i < num_rects; ++i)
  8640. rects[i].was_packed = !(rects[i].x == NK_RP__MAXVAL && rects[i].y == NK_RP__MAXVAL);
  8641. }
  8642. /*
  8643. * ==============================================================
  8644. *
  8645. * TRUETYPE
  8646. *
  8647. * ===============================================================
  8648. */
  8649. /* stb_truetype.h - v1.07 - public domain */
  8650. #define NK_TT_MAX_OVERSAMPLE 8
  8651. #define NK_TT__OVER_MASK (NK_TT_MAX_OVERSAMPLE-1)
  8652. struct nk_tt_bakedchar {
  8653. unsigned short x0,y0,x1,y1;
  8654. /* coordinates of bbox in bitmap */
  8655. float xoff,yoff,xadvance;
  8656. };
  8657. struct nk_tt_aligned_quad{
  8658. float x0,y0,s0,t0; /* top-left */
  8659. float x1,y1,s1,t1; /* bottom-right */
  8660. };
  8661. struct nk_tt_packedchar {
  8662. unsigned short x0,y0,x1,y1;
  8663. /* coordinates of bbox in bitmap */
  8664. float xoff,yoff,xadvance;
  8665. float xoff2,yoff2;
  8666. };
  8667. struct nk_tt_pack_range {
  8668. float font_size;
  8669. int first_unicode_codepoint_in_range;
  8670. /* if non-zero, then the chars are continuous, and this is the first codepoint */
  8671. int *array_of_unicode_codepoints;
  8672. /* if non-zero, then this is an array of unicode codepoints */
  8673. int num_chars;
  8674. struct nk_tt_packedchar *chardata_for_range; /* output */
  8675. unsigned char h_oversample, v_oversample;
  8676. /* don't set these, they're used internally */
  8677. };
  8678. struct nk_tt_pack_context {
  8679. void *pack_info;
  8680. int width;
  8681. int height;
  8682. int stride_in_bytes;
  8683. int padding;
  8684. unsigned int h_oversample, v_oversample;
  8685. unsigned char *pixels;
  8686. void *nodes;
  8687. };
  8688. struct nk_tt_fontinfo {
  8689. const unsigned char* data; /* pointer to .ttf file */
  8690. int fontstart;/* offset of start of font */
  8691. int numGlyphs;/* number of glyphs, needed for range checking */
  8692. int loca,head,glyf,hhea,hmtx,kern; /* table locations as offset from start of .ttf */
  8693. int index_map; /* a cmap mapping for our chosen character encoding */
  8694. int indexToLocFormat; /* format needed to map from glyph index to glyph */
  8695. };
  8696. enum {
  8697. NK_TT_vmove=1,
  8698. NK_TT_vline,
  8699. NK_TT_vcurve
  8700. };
  8701. struct nk_tt_vertex {
  8702. short x,y,cx,cy;
  8703. unsigned char type,padding;
  8704. };
  8705. struct nk_tt__bitmap{
  8706. int w,h,stride;
  8707. unsigned char *pixels;
  8708. };
  8709. struct nk_tt__hheap_chunk {
  8710. struct nk_tt__hheap_chunk *next;
  8711. };
  8712. struct nk_tt__hheap {
  8713. struct nk_allocator alloc;
  8714. struct nk_tt__hheap_chunk *head;
  8715. void *first_free;
  8716. int num_remaining_in_head_chunk;
  8717. };
  8718. struct nk_tt__edge {
  8719. float x0,y0, x1,y1;
  8720. int invert;
  8721. };
  8722. struct nk_tt__active_edge {
  8723. struct nk_tt__active_edge *next;
  8724. float fx,fdx,fdy;
  8725. float direction;
  8726. float sy;
  8727. float ey;
  8728. };
  8729. struct nk_tt__point {float x,y;};
  8730. #define NK_TT_MACSTYLE_DONTCARE 0
  8731. #define NK_TT_MACSTYLE_BOLD 1
  8732. #define NK_TT_MACSTYLE_ITALIC 2
  8733. #define NK_TT_MACSTYLE_UNDERSCORE 4
  8734. #define NK_TT_MACSTYLE_NONE 8
  8735. /* <= not same as 0, this makes us check the bitfield is 0 */
  8736. enum { /* platformID */
  8737. NK_TT_PLATFORM_ID_UNICODE =0,
  8738. NK_TT_PLATFORM_ID_MAC =1,
  8739. NK_TT_PLATFORM_ID_ISO =2,
  8740. NK_TT_PLATFORM_ID_MICROSOFT =3
  8741. };
  8742. enum { /* encodingID for NK_TT_PLATFORM_ID_UNICODE */
  8743. NK_TT_UNICODE_EID_UNICODE_1_0 =0,
  8744. NK_TT_UNICODE_EID_UNICODE_1_1 =1,
  8745. NK_TT_UNICODE_EID_ISO_10646 =2,
  8746. NK_TT_UNICODE_EID_UNICODE_2_0_BMP=3,
  8747. NK_TT_UNICODE_EID_UNICODE_2_0_FULL=4
  8748. };
  8749. enum { /* encodingID for NK_TT_PLATFORM_ID_MICROSOFT */
  8750. NK_TT_MS_EID_SYMBOL =0,
  8751. NK_TT_MS_EID_UNICODE_BMP =1,
  8752. NK_TT_MS_EID_SHIFTJIS =2,
  8753. NK_TT_MS_EID_UNICODE_FULL =10
  8754. };
  8755. enum { /* encodingID for NK_TT_PLATFORM_ID_MAC; same as Script Manager codes */
  8756. NK_TT_MAC_EID_ROMAN =0, NK_TT_MAC_EID_ARABIC =4,
  8757. NK_TT_MAC_EID_JAPANESE =1, NK_TT_MAC_EID_HEBREW =5,
  8758. NK_TT_MAC_EID_CHINESE_TRAD =2, NK_TT_MAC_EID_GREEK =6,
  8759. NK_TT_MAC_EID_KOREAN =3, NK_TT_MAC_EID_RUSSIAN =7
  8760. };
  8761. enum { /* languageID for NK_TT_PLATFORM_ID_MICROSOFT; same as LCID... */
  8762. /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
  8763. NK_TT_MS_LANG_ENGLISH =0x0409, NK_TT_MS_LANG_ITALIAN =0x0410,
  8764. NK_TT_MS_LANG_CHINESE =0x0804, NK_TT_MS_LANG_JAPANESE =0x0411,
  8765. NK_TT_MS_LANG_DUTCH =0x0413, NK_TT_MS_LANG_KOREAN =0x0412,
  8766. NK_TT_MS_LANG_FRENCH =0x040c, NK_TT_MS_LANG_RUSSIAN =0x0419,
  8767. NK_TT_MS_LANG_GERMAN =0x0407, NK_TT_MS_LANG_SPANISH =0x0409,
  8768. NK_TT_MS_LANG_HEBREW =0x040d, NK_TT_MS_LANG_SWEDISH =0x041D
  8769. };
  8770. enum { /* languageID for NK_TT_PLATFORM_ID_MAC */
  8771. NK_TT_MAC_LANG_ENGLISH =0 , NK_TT_MAC_LANG_JAPANESE =11,
  8772. NK_TT_MAC_LANG_ARABIC =12, NK_TT_MAC_LANG_KOREAN =23,
  8773. NK_TT_MAC_LANG_DUTCH =4 , NK_TT_MAC_LANG_RUSSIAN =32,
  8774. NK_TT_MAC_LANG_FRENCH =1 , NK_TT_MAC_LANG_SPANISH =6 ,
  8775. NK_TT_MAC_LANG_GERMAN =2 , NK_TT_MAC_LANG_SWEDISH =5 ,
  8776. NK_TT_MAC_LANG_HEBREW =10, NK_TT_MAC_LANG_CHINESE_SIMPLIFIED =33,
  8777. NK_TT_MAC_LANG_ITALIAN =3 , NK_TT_MAC_LANG_CHINESE_TRAD =19
  8778. };
  8779. #define nk_ttBYTE(p) (* (const nk_byte *) (p))
  8780. #define nk_ttCHAR(p) (* (const char *) (p))
  8781. #if defined(NK_BIGENDIAN) && !defined(NK_ALLOW_UNALIGNED_TRUETYPE)
  8782. #define nk_ttUSHORT(p) (* (nk_ushort *) (p))
  8783. #define nk_ttSHORT(p) (* (nk_short *) (p))
  8784. #define nk_ttULONG(p) (* (nk_uint *) (p))
  8785. #define nk_ttLONG(p) (* (nk_int *) (p))
  8786. #else
  8787. static nk_ushort nk_ttUSHORT(const nk_byte *p) { return (nk_ushort)(p[0]*256 + p[1]); }
  8788. static nk_short nk_ttSHORT(const nk_byte *p) { return (nk_short)(p[0]*256 + p[1]); }
  8789. static nk_uint nk_ttULONG(const nk_byte *p) { return (nk_uint)((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]); }
  8790. #endif
  8791. #define nk_tt_tag4(p,c0,c1,c2,c3)\
  8792. ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
  8793. #define nk_tt_tag(p,str) nk_tt_tag4(p,str[0],str[1],str[2],str[3])
  8794. NK_INTERN int nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  8795. int glyph_index, struct nk_tt_vertex **pvertices);
  8796. NK_INTERN nk_uint
  8797. nk_tt__find_table(const nk_byte *data, nk_uint fontstart, const char *tag)
  8798. {
  8799. /* @OPTIMIZE: binary search */
  8800. nk_int num_tables = nk_ttUSHORT(data+fontstart+4);
  8801. nk_uint tabledir = fontstart + 12;
  8802. nk_int i;
  8803. for (i = 0; i < num_tables; ++i) {
  8804. nk_uint loc = tabledir + (nk_uint)(16*i);
  8805. if (nk_tt_tag(data+loc+0, tag))
  8806. return nk_ttULONG(data+loc+8);
  8807. }
  8808. return 0;
  8809. }
  8810. NK_INTERN int
  8811. nk_tt_InitFont(struct nk_tt_fontinfo *info, const unsigned char *data2, int fontstart)
  8812. {
  8813. nk_uint cmap, t;
  8814. nk_int i,numTables;
  8815. const nk_byte *data = (const nk_byte *) data2;
  8816. info->data = data;
  8817. info->fontstart = fontstart;
  8818. cmap = nk_tt__find_table(data, (nk_uint)fontstart, "cmap"); /* required */
  8819. info->loca = (int)nk_tt__find_table(data, (nk_uint)fontstart, "loca"); /* required */
  8820. info->head = (int)nk_tt__find_table(data, (nk_uint)fontstart, "head"); /* required */
  8821. info->glyf = (int)nk_tt__find_table(data, (nk_uint)fontstart, "glyf"); /* required */
  8822. info->hhea = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hhea"); /* required */
  8823. info->hmtx = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hmtx"); /* required */
  8824. info->kern = (int)nk_tt__find_table(data, (nk_uint)fontstart, "kern"); /* not required */
  8825. if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
  8826. return 0;
  8827. t = nk_tt__find_table(data, (nk_uint)fontstart, "maxp");
  8828. if (t) info->numGlyphs = nk_ttUSHORT(data+t+4);
  8829. else info->numGlyphs = 0xffff;
  8830. /* find a cmap encoding table we understand *now* to avoid searching */
  8831. /* later. (todo: could make this installable) */
  8832. /* the same regardless of glyph. */
  8833. numTables = nk_ttUSHORT(data + cmap + 2);
  8834. info->index_map = 0;
  8835. for (i=0; i < numTables; ++i)
  8836. {
  8837. nk_uint encoding_record = cmap + 4 + 8 * (nk_uint)i;
  8838. /* find an encoding we understand: */
  8839. switch(nk_ttUSHORT(data+encoding_record)) {
  8840. case NK_TT_PLATFORM_ID_MICROSOFT:
  8841. switch (nk_ttUSHORT(data+encoding_record+2)) {
  8842. case NK_TT_MS_EID_UNICODE_BMP:
  8843. case NK_TT_MS_EID_UNICODE_FULL:
  8844. /* MS/Unicode */
  8845. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  8846. break;
  8847. default: break;
  8848. } break;
  8849. case NK_TT_PLATFORM_ID_UNICODE:
  8850. /* Mac/iOS has these */
  8851. /* all the encodingIDs are unicode, so we don't bother to check it */
  8852. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  8853. break;
  8854. default: break;
  8855. }
  8856. }
  8857. if (info->index_map == 0)
  8858. return 0;
  8859. info->indexToLocFormat = nk_ttUSHORT(data+info->head + 50);
  8860. return 1;
  8861. }
  8862. NK_INTERN int
  8863. nk_tt_FindGlyphIndex(const struct nk_tt_fontinfo *info, int unicode_codepoint)
  8864. {
  8865. const nk_byte *data = info->data;
  8866. nk_uint index_map = (nk_uint)info->index_map;
  8867. nk_ushort format = nk_ttUSHORT(data + index_map + 0);
  8868. if (format == 0) { /* apple byte encoding */
  8869. nk_int bytes = nk_ttUSHORT(data + index_map + 2);
  8870. if (unicode_codepoint < bytes-6)
  8871. return nk_ttBYTE(data + index_map + 6 + unicode_codepoint);
  8872. return 0;
  8873. } else if (format == 6) {
  8874. nk_uint first = nk_ttUSHORT(data + index_map + 6);
  8875. nk_uint count = nk_ttUSHORT(data + index_map + 8);
  8876. if ((nk_uint) unicode_codepoint >= first && (nk_uint) unicode_codepoint < first+count)
  8877. return nk_ttUSHORT(data + index_map + 10 + (unicode_codepoint - (int)first)*2);
  8878. return 0;
  8879. } else if (format == 2) {
  8880. NK_ASSERT(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
  8881. return 0;
  8882. } else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
  8883. nk_ushort segcount = nk_ttUSHORT(data+index_map+6) >> 1;
  8884. nk_ushort searchRange = nk_ttUSHORT(data+index_map+8) >> 1;
  8885. nk_ushort entrySelector = nk_ttUSHORT(data+index_map+10);
  8886. nk_ushort rangeShift = nk_ttUSHORT(data+index_map+12) >> 1;
  8887. /* do a binary search of the segments */
  8888. nk_uint endCount = index_map + 14;
  8889. nk_uint search = endCount;
  8890. if (unicode_codepoint > 0xffff)
  8891. return 0;
  8892. /* they lie from endCount .. endCount + segCount */
  8893. /* but searchRange is the nearest power of two, so... */
  8894. if (unicode_codepoint >= nk_ttUSHORT(data + search + rangeShift*2))
  8895. search += (nk_uint)(rangeShift*2);
  8896. /* now decrement to bias correctly to find smallest */
  8897. search -= 2;
  8898. while (entrySelector) {
  8899. nk_ushort end;
  8900. searchRange >>= 1;
  8901. end = nk_ttUSHORT(data + search + searchRange*2);
  8902. if (unicode_codepoint > end)
  8903. search += (nk_uint)(searchRange*2);
  8904. --entrySelector;
  8905. }
  8906. search += 2;
  8907. {
  8908. nk_ushort offset, start;
  8909. nk_ushort item = (nk_ushort) ((search - endCount) >> 1);
  8910. NK_ASSERT(unicode_codepoint <= nk_ttUSHORT(data + endCount + 2*item));
  8911. start = nk_ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
  8912. if (unicode_codepoint < start)
  8913. return 0;
  8914. offset = nk_ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
  8915. if (offset == 0)
  8916. return (nk_ushort) (unicode_codepoint + nk_ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
  8917. return nk_ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
  8918. }
  8919. } else if (format == 12 || format == 13) {
  8920. nk_uint ngroups = nk_ttULONG(data+index_map+12);
  8921. nk_int low,high;
  8922. low = 0; high = (nk_int)ngroups;
  8923. /* Binary search the right group. */
  8924. while (low < high) {
  8925. nk_int mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
  8926. nk_uint start_char = nk_ttULONG(data+index_map+16+mid*12);
  8927. nk_uint end_char = nk_ttULONG(data+index_map+16+mid*12+4);
  8928. if ((nk_uint) unicode_codepoint < start_char)
  8929. high = mid;
  8930. else if ((nk_uint) unicode_codepoint > end_char)
  8931. low = mid+1;
  8932. else {
  8933. nk_uint start_glyph = nk_ttULONG(data+index_map+16+mid*12+8);
  8934. if (format == 12)
  8935. return (int)start_glyph + (int)unicode_codepoint - (int)start_char;
  8936. else /* format == 13 */
  8937. return (int)start_glyph;
  8938. }
  8939. }
  8940. return 0; /* not found */
  8941. }
  8942. /* @TODO */
  8943. NK_ASSERT(0);
  8944. return 0;
  8945. }
  8946. NK_INTERN void
  8947. nk_tt_setvertex(struct nk_tt_vertex *v, nk_byte type, nk_int x, nk_int y, nk_int cx, nk_int cy)
  8948. {
  8949. v->type = type;
  8950. v->x = (nk_short) x;
  8951. v->y = (nk_short) y;
  8952. v->cx = (nk_short) cx;
  8953. v->cy = (nk_short) cy;
  8954. }
  8955. NK_INTERN int
  8956. nk_tt__GetGlyfOffset(const struct nk_tt_fontinfo *info, int glyph_index)
  8957. {
  8958. int g1,g2;
  8959. if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
  8960. if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
  8961. if (info->indexToLocFormat == 0) {
  8962. g1 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
  8963. g2 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
  8964. } else {
  8965. g1 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4);
  8966. g2 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4 + 4);
  8967. }
  8968. return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
  8969. }
  8970. NK_INTERN int
  8971. nk_tt_GetGlyphBox(const struct nk_tt_fontinfo *info, int glyph_index,
  8972. int *x0, int *y0, int *x1, int *y1)
  8973. {
  8974. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  8975. if (g < 0) return 0;
  8976. if (x0) *x0 = nk_ttSHORT(info->data + g + 2);
  8977. if (y0) *y0 = nk_ttSHORT(info->data + g + 4);
  8978. if (x1) *x1 = nk_ttSHORT(info->data + g + 6);
  8979. if (y1) *y1 = nk_ttSHORT(info->data + g + 8);
  8980. return 1;
  8981. }
  8982. NK_INTERN int
  8983. stbtt__close_shape(struct nk_tt_vertex *vertices, int num_vertices, int was_off,
  8984. int start_off, nk_int sx, nk_int sy, nk_int scx, nk_int scy, nk_int cx, nk_int cy)
  8985. {
  8986. if (start_off) {
  8987. if (was_off)
  8988. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
  8989. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, sx,sy,scx,scy);
  8990. } else {
  8991. if (was_off)
  8992. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve,sx,sy,cx,cy);
  8993. else
  8994. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline,sx,sy,0,0);
  8995. }
  8996. return num_vertices;
  8997. }
  8998. NK_INTERN int
  8999. nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  9000. int glyph_index, struct nk_tt_vertex **pvertices)
  9001. {
  9002. nk_short numberOfContours;
  9003. const nk_byte *endPtsOfContours;
  9004. const nk_byte *data = info->data;
  9005. struct nk_tt_vertex *vertices=0;
  9006. int num_vertices=0;
  9007. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  9008. *pvertices = 0;
  9009. if (g < 0) return 0;
  9010. numberOfContours = nk_ttSHORT(data + g);
  9011. if (numberOfContours > 0) {
  9012. nk_byte flags=0,flagcount;
  9013. nk_int ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
  9014. nk_int x,y,cx,cy,sx,sy, scx,scy;
  9015. const nk_byte *points;
  9016. endPtsOfContours = (data + g + 10);
  9017. ins = nk_ttUSHORT(data + g + 10 + numberOfContours * 2);
  9018. points = data + g + 10 + numberOfContours * 2 + 2 + ins;
  9019. n = 1+nk_ttUSHORT(endPtsOfContours + numberOfContours*2-2);
  9020. m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
  9021. vertices = (struct nk_tt_vertex *)alloc->alloc(alloc->userdata, 0, (nk_size)m * sizeof(vertices[0]));
  9022. if (vertices == 0)
  9023. return 0;
  9024. next_move = 0;
  9025. flagcount=0;
  9026. /* in first pass, we load uninterpreted data into the allocated array */
  9027. /* above, shifted to the end of the array so we won't overwrite it when */
  9028. /* we create our final data starting from the front */
  9029. off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
  9030. /* first load flags */
  9031. for (i=0; i < n; ++i) {
  9032. if (flagcount == 0) {
  9033. flags = *points++;
  9034. if (flags & 8)
  9035. flagcount = *points++;
  9036. } else --flagcount;
  9037. vertices[off+i].type = flags;
  9038. }
  9039. /* now load x coordinates */
  9040. x=0;
  9041. for (i=0; i < n; ++i) {
  9042. flags = vertices[off+i].type;
  9043. if (flags & 2) {
  9044. nk_short dx = *points++;
  9045. x += (flags & 16) ? dx : -dx; /* ??? */
  9046. } else {
  9047. if (!(flags & 16)) {
  9048. x = x + (nk_short) (points[0]*256 + points[1]);
  9049. points += 2;
  9050. }
  9051. }
  9052. vertices[off+i].x = (nk_short) x;
  9053. }
  9054. /* now load y coordinates */
  9055. y=0;
  9056. for (i=0; i < n; ++i) {
  9057. flags = vertices[off+i].type;
  9058. if (flags & 4) {
  9059. nk_short dy = *points++;
  9060. y += (flags & 32) ? dy : -dy; /* ??? */
  9061. } else {
  9062. if (!(flags & 32)) {
  9063. y = y + (nk_short) (points[0]*256 + points[1]);
  9064. points += 2;
  9065. }
  9066. }
  9067. vertices[off+i].y = (nk_short) y;
  9068. }
  9069. /* now convert them to our format */
  9070. num_vertices=0;
  9071. sx = sy = cx = cy = scx = scy = 0;
  9072. for (i=0; i < n; ++i)
  9073. {
  9074. flags = vertices[off+i].type;
  9075. x = (nk_short) vertices[off+i].x;
  9076. y = (nk_short) vertices[off+i].y;
  9077. if (next_move == i) {
  9078. if (i != 0)
  9079. num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  9080. /* now start the new one */
  9081. start_off = !(flags & 1);
  9082. if (start_off) {
  9083. /* if we start off with an off-curve point, then when we need to find a point on the curve */
  9084. /* where we can start, and we need to save some state for when we wraparound. */
  9085. scx = x;
  9086. scy = y;
  9087. if (!(vertices[off+i+1].type & 1)) {
  9088. /* next point is also a curve point, so interpolate an on-point curve */
  9089. sx = (x + (nk_int) vertices[off+i+1].x) >> 1;
  9090. sy = (y + (nk_int) vertices[off+i+1].y) >> 1;
  9091. } else {
  9092. /* otherwise just use the next point as our start point */
  9093. sx = (nk_int) vertices[off+i+1].x;
  9094. sy = (nk_int) vertices[off+i+1].y;
  9095. ++i; /* we're using point i+1 as the starting point, so skip it */
  9096. }
  9097. } else {
  9098. sx = x;
  9099. sy = y;
  9100. }
  9101. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vmove,sx,sy,0,0);
  9102. was_off = 0;
  9103. next_move = 1 + nk_ttUSHORT(endPtsOfContours+j*2);
  9104. ++j;
  9105. } else {
  9106. if (!(flags & 1))
  9107. { /* if it's a curve */
  9108. if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
  9109. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
  9110. cx = x;
  9111. cy = y;
  9112. was_off = 1;
  9113. } else {
  9114. if (was_off)
  9115. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, x,y, cx, cy);
  9116. else nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline, x,y,0,0);
  9117. was_off = 0;
  9118. }
  9119. }
  9120. }
  9121. num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  9122. } else if (numberOfContours == -1) {
  9123. /* Compound shapes. */
  9124. int more = 1;
  9125. const nk_byte *comp = data + g + 10;
  9126. num_vertices = 0;
  9127. vertices = 0;
  9128. while (more)
  9129. {
  9130. nk_ushort flags, gidx;
  9131. int comp_num_verts = 0, i;
  9132. struct nk_tt_vertex *comp_verts = 0, *tmp = 0;
  9133. float mtx[6] = {1,0,0,1,0,0}, m, n;
  9134. flags = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  9135. gidx = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  9136. if (flags & 2) { /* XY values */
  9137. if (flags & 1) { /* shorts */
  9138. mtx[4] = nk_ttSHORT(comp); comp+=2;
  9139. mtx[5] = nk_ttSHORT(comp); comp+=2;
  9140. } else {
  9141. mtx[4] = nk_ttCHAR(comp); comp+=1;
  9142. mtx[5] = nk_ttCHAR(comp); comp+=1;
  9143. }
  9144. } else {
  9145. /* @TODO handle matching point */
  9146. NK_ASSERT(0);
  9147. }
  9148. if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
  9149. mtx[0] = mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9150. mtx[1] = mtx[2] = 0;
  9151. } else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
  9152. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9153. mtx[1] = mtx[2] = 0;
  9154. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9155. } else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
  9156. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9157. mtx[1] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9158. mtx[2] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9159. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  9160. }
  9161. /* Find transformation scales. */
  9162. m = (float) NK_SQRT(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
  9163. n = (float) NK_SQRT(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
  9164. /* Get indexed glyph. */
  9165. comp_num_verts = nk_tt_GetGlyphShape(info, alloc, gidx, &comp_verts);
  9166. if (comp_num_verts > 0)
  9167. {
  9168. /* Transform vertices. */
  9169. for (i = 0; i < comp_num_verts; ++i) {
  9170. struct nk_tt_vertex* v = &comp_verts[i];
  9171. short x,y;
  9172. x=v->x; y=v->y;
  9173. v->x = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  9174. v->y = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  9175. x=v->cx; y=v->cy;
  9176. v->cx = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  9177. v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  9178. }
  9179. /* Append vertices. */
  9180. tmp = (struct nk_tt_vertex*)alloc->alloc(alloc->userdata, 0,
  9181. (nk_size)(num_vertices+comp_num_verts)*sizeof(struct nk_tt_vertex));
  9182. if (!tmp) {
  9183. if (vertices) alloc->free(alloc->userdata, vertices);
  9184. if (comp_verts) alloc->free(alloc->userdata, comp_verts);
  9185. return 0;
  9186. }
  9187. if (num_vertices > 0) NK_MEMCPY(tmp, vertices, (nk_size)num_vertices*sizeof(struct nk_tt_vertex));
  9188. NK_MEMCPY(tmp+num_vertices, comp_verts, (nk_size)comp_num_verts*sizeof(struct nk_tt_vertex));
  9189. if (vertices) alloc->free(alloc->userdata,vertices);
  9190. vertices = tmp;
  9191. alloc->free(alloc->userdata,comp_verts);
  9192. num_vertices += comp_num_verts;
  9193. }
  9194. /* More components ? */
  9195. more = flags & (1<<5);
  9196. }
  9197. } else if (numberOfContours < 0) {
  9198. /* @TODO other compound variations? */
  9199. NK_ASSERT(0);
  9200. } else {
  9201. /* numberOfCounters == 0, do nothing */
  9202. }
  9203. *pvertices = vertices;
  9204. return num_vertices;
  9205. }
  9206. NK_INTERN void
  9207. nk_tt_GetGlyphHMetrics(const struct nk_tt_fontinfo *info, int glyph_index,
  9208. int *advanceWidth, int *leftSideBearing)
  9209. {
  9210. nk_ushort numOfLongHorMetrics = nk_ttUSHORT(info->data+info->hhea + 34);
  9211. if (glyph_index < numOfLongHorMetrics) {
  9212. if (advanceWidth)
  9213. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index);
  9214. if (leftSideBearing)
  9215. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
  9216. } else {
  9217. if (advanceWidth)
  9218. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
  9219. if (leftSideBearing)
  9220. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
  9221. }
  9222. }
  9223. NK_INTERN void
  9224. nk_tt_GetFontVMetrics(const struct nk_tt_fontinfo *info,
  9225. int *ascent, int *descent, int *lineGap)
  9226. {
  9227. if (ascent ) *ascent = nk_ttSHORT(info->data+info->hhea + 4);
  9228. if (descent) *descent = nk_ttSHORT(info->data+info->hhea + 6);
  9229. if (lineGap) *lineGap = nk_ttSHORT(info->data+info->hhea + 8);
  9230. }
  9231. NK_INTERN float
  9232. nk_tt_ScaleForPixelHeight(const struct nk_tt_fontinfo *info, float height)
  9233. {
  9234. int fheight = nk_ttSHORT(info->data + info->hhea + 4) - nk_ttSHORT(info->data + info->hhea + 6);
  9235. return (float) height / (float)fheight;
  9236. }
  9237. NK_INTERN float
  9238. nk_tt_ScaleForMappingEmToPixels(const struct nk_tt_fontinfo *info, float pixels)
  9239. {
  9240. int unitsPerEm = nk_ttUSHORT(info->data + info->head + 18);
  9241. return pixels / (float)unitsPerEm;
  9242. }
  9243. /*-------------------------------------------------------------
  9244. * antialiasing software rasterizer
  9245. * --------------------------------------------------------------*/
  9246. NK_INTERN void
  9247. nk_tt_GetGlyphBitmapBoxSubpixel(const struct nk_tt_fontinfo *font,
  9248. int glyph, float scale_x, float scale_y,float shift_x, float shift_y,
  9249. int *ix0, int *iy0, int *ix1, int *iy1)
  9250. {
  9251. int x0,y0,x1,y1;
  9252. if (!nk_tt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
  9253. /* e.g. space character */
  9254. if (ix0) *ix0 = 0;
  9255. if (iy0) *iy0 = 0;
  9256. if (ix1) *ix1 = 0;
  9257. if (iy1) *iy1 = 0;
  9258. } else {
  9259. /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
  9260. if (ix0) *ix0 = nk_ifloorf((float)x0 * scale_x + shift_x);
  9261. if (iy0) *iy0 = nk_ifloorf((float)-y1 * scale_y + shift_y);
  9262. if (ix1) *ix1 = nk_iceilf ((float)x1 * scale_x + shift_x);
  9263. if (iy1) *iy1 = nk_iceilf ((float)-y0 * scale_y + shift_y);
  9264. }
  9265. }
  9266. NK_INTERN void
  9267. nk_tt_GetGlyphBitmapBox(const struct nk_tt_fontinfo *font, int glyph,
  9268. float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
  9269. {
  9270. nk_tt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
  9271. }
  9272. /*-------------------------------------------------------------
  9273. * Rasterizer
  9274. * --------------------------------------------------------------*/
  9275. NK_INTERN void*
  9276. nk_tt__hheap_alloc(struct nk_tt__hheap *hh, nk_size size)
  9277. {
  9278. if (hh->first_free) {
  9279. void *p = hh->first_free;
  9280. hh->first_free = * (void **) p;
  9281. return p;
  9282. } else {
  9283. if (hh->num_remaining_in_head_chunk == 0) {
  9284. int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
  9285. struct nk_tt__hheap_chunk *c = (struct nk_tt__hheap_chunk *)
  9286. hh->alloc.alloc(hh->alloc.userdata, 0,
  9287. sizeof(struct nk_tt__hheap_chunk) + size * (nk_size)count);
  9288. if (c == 0) return 0;
  9289. c->next = hh->head;
  9290. hh->head = c;
  9291. hh->num_remaining_in_head_chunk = count;
  9292. }
  9293. --hh->num_remaining_in_head_chunk;
  9294. return (char *) (hh->head) + size * (nk_size)hh->num_remaining_in_head_chunk;
  9295. }
  9296. }
  9297. NK_INTERN void
  9298. nk_tt__hheap_free(struct nk_tt__hheap *hh, void *p)
  9299. {
  9300. *(void **) p = hh->first_free;
  9301. hh->first_free = p;
  9302. }
  9303. NK_INTERN void
  9304. nk_tt__hheap_cleanup(struct nk_tt__hheap *hh)
  9305. {
  9306. struct nk_tt__hheap_chunk *c = hh->head;
  9307. while (c) {
  9308. struct nk_tt__hheap_chunk *n = c->next;
  9309. hh->alloc.free(hh->alloc.userdata, c);
  9310. c = n;
  9311. }
  9312. }
  9313. NK_INTERN struct nk_tt__active_edge*
  9314. nk_tt__new_active(struct nk_tt__hheap *hh, struct nk_tt__edge *e,
  9315. int off_x, float start_point)
  9316. {
  9317. struct nk_tt__active_edge *z = (struct nk_tt__active_edge *)
  9318. nk_tt__hheap_alloc(hh, sizeof(*z));
  9319. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  9320. /*STBTT_assert(e->y0 <= start_point); */
  9321. if (!z) return z;
  9322. z->fdx = dxdy;
  9323. z->fdy = (dxdy != 0) ? (1/dxdy): 0;
  9324. z->fx = e->x0 + dxdy * (start_point - e->y0);
  9325. z->fx -= (float)off_x;
  9326. z->direction = e->invert ? 1.0f : -1.0f;
  9327. z->sy = e->y0;
  9328. z->ey = e->y1;
  9329. z->next = 0;
  9330. return z;
  9331. }
  9332. NK_INTERN void
  9333. nk_tt__handle_clipped_edge(float *scanline, int x, struct nk_tt__active_edge *e,
  9334. float x0, float y0, float x1, float y1)
  9335. {
  9336. if (y0 == y1) return;
  9337. NK_ASSERT(y0 < y1);
  9338. NK_ASSERT(e->sy <= e->ey);
  9339. if (y0 > e->ey) return;
  9340. if (y1 < e->sy) return;
  9341. if (y0 < e->sy) {
  9342. x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
  9343. y0 = e->sy;
  9344. }
  9345. if (y1 > e->ey) {
  9346. x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
  9347. y1 = e->ey;
  9348. }
  9349. if (x0 == x) NK_ASSERT(x1 <= x+1);
  9350. else if (x0 == x+1) NK_ASSERT(x1 >= x);
  9351. else if (x0 <= x) NK_ASSERT(x1 <= x);
  9352. else if (x0 >= x+1) NK_ASSERT(x1 >= x+1);
  9353. else NK_ASSERT(x1 >= x && x1 <= x+1);
  9354. if (x0 <= x && x1 <= x)
  9355. scanline[x] += e->direction * (y1-y0);
  9356. else if (x0 >= x+1 && x1 >= x+1);
  9357. else {
  9358. NK_ASSERT(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
  9359. /* coverage = 1 - average x position */
  9360. scanline[x] += (float)e->direction * (float)(y1-y0) * (1.0f-((x0-(float)x)+(x1-(float)x))/2.0f);
  9361. }
  9362. }
  9363. NK_INTERN void
  9364. nk_tt__fill_active_edges_new(float *scanline, float *scanline_fill, int len,
  9365. struct nk_tt__active_edge *e, float y_top)
  9366. {
  9367. float y_bottom = y_top+1;
  9368. while (e)
  9369. {
  9370. /* brute force every pixel */
  9371. /* compute intersection points with top & bottom */
  9372. NK_ASSERT(e->ey >= y_top);
  9373. if (e->fdx == 0) {
  9374. float x0 = e->fx;
  9375. if (x0 < len) {
  9376. if (x0 >= 0) {
  9377. nk_tt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
  9378. nk_tt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
  9379. } else {
  9380. nk_tt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
  9381. }
  9382. }
  9383. } else {
  9384. float x0 = e->fx;
  9385. float dx = e->fdx;
  9386. float xb = x0 + dx;
  9387. float x_top, x_bottom;
  9388. float y0,y1;
  9389. float dy = e->fdy;
  9390. NK_ASSERT(e->sy <= y_bottom && e->ey >= y_top);
  9391. /* compute endpoints of line segment clipped to this scanline (if the */
  9392. /* line segment starts on this scanline. x0 is the intersection of the */
  9393. /* line with y_top, but that may be off the line segment. */
  9394. if (e->sy > y_top) {
  9395. x_top = x0 + dx * (e->sy - y_top);
  9396. y0 = e->sy;
  9397. } else {
  9398. x_top = x0;
  9399. y0 = y_top;
  9400. }
  9401. if (e->ey < y_bottom) {
  9402. x_bottom = x0 + dx * (e->ey - y_top);
  9403. y1 = e->ey;
  9404. } else {
  9405. x_bottom = xb;
  9406. y1 = y_bottom;
  9407. }
  9408. if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len)
  9409. {
  9410. /* from here on, we don't have to range check x values */
  9411. if ((int) x_top == (int) x_bottom) {
  9412. float height;
  9413. /* simple case, only spans one pixel */
  9414. int x = (int) x_top;
  9415. height = y1 - y0;
  9416. NK_ASSERT(x >= 0 && x < len);
  9417. scanline[x] += e->direction * (1.0f-(((float)x_top - (float)x) + ((float)x_bottom-(float)x))/2.0f) * (float)height;
  9418. scanline_fill[x] += e->direction * (float)height; /* everything right of this pixel is filled */
  9419. } else {
  9420. int x,x1,x2;
  9421. float y_crossing, step, sign, area;
  9422. /* covers 2+ pixels */
  9423. if (x_top > x_bottom)
  9424. {
  9425. /* flip scanline vertically; signed area is the same */
  9426. float t;
  9427. y0 = y_bottom - (y0 - y_top);
  9428. y1 = y_bottom - (y1 - y_top);
  9429. t = y0; y0 = y1; y1 = t;
  9430. t = x_bottom; x_bottom = x_top; x_top = t;
  9431. dx = -dx;
  9432. dy = -dy;
  9433. t = x0; x0 = xb; xb = t;
  9434. }
  9435. x1 = (int) x_top;
  9436. x2 = (int) x_bottom;
  9437. /* compute intersection with y axis at x1+1 */
  9438. y_crossing = ((float)x1+1 - (float)x0) * (float)dy + (float)y_top;
  9439. sign = e->direction;
  9440. /* area of the rectangle covered from y0..y_crossing */
  9441. area = sign * (y_crossing-y0);
  9442. /* area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) */
  9443. scanline[x1] += area * (1.0f-((float)((float)x_top - (float)x1)+(float)(x1+1-x1))/2.0f);
  9444. step = sign * dy;
  9445. for (x = x1+1; x < x2; ++x) {
  9446. scanline[x] += area + step/2;
  9447. area += step;
  9448. }
  9449. y_crossing += (float)dy * (float)(x2 - (x1+1));
  9450. scanline[x2] += area + sign * (1.0f-((float)(x2-x2)+((float)x_bottom-(float)x2))/2.0f) * (y1-y_crossing);
  9451. scanline_fill[x2] += sign * (y1-y0);
  9452. }
  9453. }
  9454. else
  9455. {
  9456. /* if edge goes outside of box we're drawing, we require */
  9457. /* clipping logic. since this does not match the intended use */
  9458. /* of this library, we use a different, very slow brute */
  9459. /* force implementation */
  9460. int x;
  9461. for (x=0; x < len; ++x)
  9462. {
  9463. /* cases: */
  9464. /* */
  9465. /* there can be up to two intersections with the pixel. any intersection */
  9466. /* with left or right edges can be handled by splitting into two (or three) */
  9467. /* regions. intersections with top & bottom do not necessitate case-wise logic. */
  9468. /* */
  9469. /* the old way of doing this found the intersections with the left & right edges, */
  9470. /* then used some simple logic to produce up to three segments in sorted order */
  9471. /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
  9472. /* across the x border, then the corresponding y position might not be distinct */
  9473. /* from the other y segment, and it might ignored as an empty segment. to avoid */
  9474. /* that, we need to explicitly produce segments based on x positions. */
  9475. /* rename variables to clear pairs */
  9476. float ya = y_top;
  9477. float x1 = (float) (x);
  9478. float x2 = (float) (x+1);
  9479. float x3 = xb;
  9480. float y3 = y_bottom;
  9481. float yb,y2;
  9482. yb = ((float)x - x0) / dx + y_top;
  9483. y2 = ((float)x+1 - x0) / dx + y_top;
  9484. if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
  9485. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  9486. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x2,y2);
  9487. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  9488. } else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
  9489. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  9490. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x1,yb);
  9491. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  9492. } else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
  9493. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  9494. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  9495. } else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
  9496. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  9497. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  9498. } else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
  9499. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  9500. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  9501. } else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
  9502. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  9503. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  9504. } else { /* one segment */
  9505. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x3,y3);
  9506. }
  9507. }
  9508. }
  9509. }
  9510. e = e->next;
  9511. }
  9512. }
  9513. /* directly AA rasterize edges w/o supersampling */
  9514. NK_INTERN void
  9515. nk_tt__rasterize_sorted_edges(struct nk_tt__bitmap *result, struct nk_tt__edge *e,
  9516. int n, int vsubsample, int off_x, int off_y, struct nk_allocator *alloc)
  9517. {
  9518. struct nk_tt__hheap hh;
  9519. struct nk_tt__active_edge *active = 0;
  9520. int y,j=0, i;
  9521. float scanline_data[129], *scanline, *scanline2;
  9522. NK_UNUSED(vsubsample);
  9523. nk_zero_struct(hh);
  9524. hh.alloc = *alloc;
  9525. if (result->w > 64)
  9526. scanline = (float *) alloc->alloc(alloc->userdata,0, (nk_size)(result->w*2+1) * sizeof(float));
  9527. else scanline = scanline_data;
  9528. scanline2 = scanline + result->w;
  9529. y = off_y;
  9530. e[n].y0 = (float) (off_y + result->h) + 1;
  9531. while (j < result->h)
  9532. {
  9533. /* find center of pixel for this scanline */
  9534. float scan_y_top = (float)y + 0.0f;
  9535. float scan_y_bottom = (float)y + 1.0f;
  9536. struct nk_tt__active_edge **step = &active;
  9537. NK_MEMSET(scanline , 0, (nk_size)result->w*sizeof(scanline[0]));
  9538. NK_MEMSET(scanline2, 0, (nk_size)(result->w+1)*sizeof(scanline[0]));
  9539. /* update all active edges; */
  9540. /* remove all active edges that terminate before the top of this scanline */
  9541. while (*step) {
  9542. struct nk_tt__active_edge * z = *step;
  9543. if (z->ey <= scan_y_top) {
  9544. *step = z->next; /* delete from list */
  9545. NK_ASSERT(z->direction);
  9546. z->direction = 0;
  9547. nk_tt__hheap_free(&hh, z);
  9548. } else {
  9549. step = &((*step)->next); /* advance through list */
  9550. }
  9551. }
  9552. /* insert all edges that start before the bottom of this scanline */
  9553. while (e->y0 <= scan_y_bottom) {
  9554. if (e->y0 != e->y1) {
  9555. struct nk_tt__active_edge *z = nk_tt__new_active(&hh, e, off_x, scan_y_top);
  9556. if (z != 0) {
  9557. NK_ASSERT(z->ey >= scan_y_top);
  9558. /* insert at front */
  9559. z->next = active;
  9560. active = z;
  9561. }
  9562. }
  9563. ++e;
  9564. }
  9565. /* now process all active edges */
  9566. if (active)
  9567. nk_tt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
  9568. {
  9569. float sum = 0;
  9570. for (i=0; i < result->w; ++i) {
  9571. float k;
  9572. int m;
  9573. sum += scanline2[i];
  9574. k = scanline[i] + sum;
  9575. k = (float) NK_ABS(k) * 255.0f + 0.5f;
  9576. m = (int) k;
  9577. if (m > 255) m = 255;
  9578. result->pixels[j*result->stride + i] = (unsigned char) m;
  9579. }
  9580. }
  9581. /* advance all the edges */
  9582. step = &active;
  9583. while (*step) {
  9584. struct nk_tt__active_edge *z = *step;
  9585. z->fx += z->fdx; /* advance to position for current scanline */
  9586. step = &((*step)->next); /* advance through list */
  9587. }
  9588. ++y;
  9589. ++j;
  9590. }
  9591. nk_tt__hheap_cleanup(&hh);
  9592. if (scanline != scanline_data)
  9593. alloc->free(alloc->userdata, scanline);
  9594. }
  9595. #define NK_TT__COMPARE(a,b) ((a)->y0 < (b)->y0)
  9596. NK_INTERN void
  9597. nk_tt__sort_edges_ins_sort(struct nk_tt__edge *p, int n)
  9598. {
  9599. int i,j;
  9600. for (i=1; i < n; ++i) {
  9601. struct nk_tt__edge t = p[i], *a = &t;
  9602. j = i;
  9603. while (j > 0) {
  9604. struct nk_tt__edge *b = &p[j-1];
  9605. int c = NK_TT__COMPARE(a,b);
  9606. if (!c) break;
  9607. p[j] = p[j-1];
  9608. --j;
  9609. }
  9610. if (i != j)
  9611. p[j] = t;
  9612. }
  9613. }
  9614. NK_INTERN void
  9615. nk_tt__sort_edges_quicksort(struct nk_tt__edge *p, int n)
  9616. {
  9617. /* threshold for transitioning to insertion sort */
  9618. while (n > 12) {
  9619. struct nk_tt__edge t;
  9620. int c01,c12,c,m,i,j;
  9621. /* compute median of three */
  9622. m = n >> 1;
  9623. c01 = NK_TT__COMPARE(&p[0],&p[m]);
  9624. c12 = NK_TT__COMPARE(&p[m],&p[n-1]);
  9625. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
  9626. if (c01 != c12) {
  9627. /* otherwise, we'll need to swap something else to middle */
  9628. int z;
  9629. c = NK_TT__COMPARE(&p[0],&p[n-1]);
  9630. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
  9631. /* 0<mid && mid>n: 0>n => 0; 0<n => n */
  9632. z = (c == c12) ? 0 : n-1;
  9633. t = p[z];
  9634. p[z] = p[m];
  9635. p[m] = t;
  9636. }
  9637. /* now p[m] is the median-of-three */
  9638. /* swap it to the beginning so it won't move around */
  9639. t = p[0];
  9640. p[0] = p[m];
  9641. p[m] = t;
  9642. /* partition loop */
  9643. i=1;
  9644. j=n-1;
  9645. for(;;) {
  9646. /* handling of equality is crucial here */
  9647. /* for sentinels & efficiency with duplicates */
  9648. for (;;++i) {
  9649. if (!NK_TT__COMPARE(&p[i], &p[0])) break;
  9650. }
  9651. for (;;--j) {
  9652. if (!NK_TT__COMPARE(&p[0], &p[j])) break;
  9653. }
  9654. /* make sure we haven't crossed */
  9655. if (i >= j) break;
  9656. t = p[i];
  9657. p[i] = p[j];
  9658. p[j] = t;
  9659. ++i;
  9660. --j;
  9661. }
  9662. /* recurse on smaller side, iterate on larger */
  9663. if (j < (n-i)) {
  9664. nk_tt__sort_edges_quicksort(p,j);
  9665. p = p+i;
  9666. n = n-i;
  9667. } else {
  9668. nk_tt__sort_edges_quicksort(p+i, n-i);
  9669. n = j;
  9670. }
  9671. }
  9672. }
  9673. NK_INTERN void
  9674. nk_tt__sort_edges(struct nk_tt__edge *p, int n)
  9675. {
  9676. nk_tt__sort_edges_quicksort(p, n);
  9677. nk_tt__sort_edges_ins_sort(p, n);
  9678. }
  9679. NK_INTERN void
  9680. nk_tt__rasterize(struct nk_tt__bitmap *result, struct nk_tt__point *pts,
  9681. int *wcount, int windings, float scale_x, float scale_y,
  9682. float shift_x, float shift_y, int off_x, int off_y, int invert,
  9683. struct nk_allocator *alloc)
  9684. {
  9685. float y_scale_inv = invert ? -scale_y : scale_y;
  9686. struct nk_tt__edge *e;
  9687. int n,i,j,k,m;
  9688. int vsubsample = 1;
  9689. /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
  9690. /* now we have to blow out the windings into explicit edge lists */
  9691. n = 0;
  9692. for (i=0; i < windings; ++i)
  9693. n += wcount[i];
  9694. e = (struct nk_tt__edge*)
  9695. alloc->alloc(alloc->userdata, 0,(sizeof(*e) * (nk_size)(n+1)));
  9696. if (e == 0) return;
  9697. n = 0;
  9698. m=0;
  9699. for (i=0; i < windings; ++i)
  9700. {
  9701. struct nk_tt__point *p = pts + m;
  9702. m += wcount[i];
  9703. j = wcount[i]-1;
  9704. for (k=0; k < wcount[i]; j=k++) {
  9705. int a=k,b=j;
  9706. /* skip the edge if horizontal */
  9707. if (p[j].y == p[k].y)
  9708. continue;
  9709. /* add edge from j to k to the list */
  9710. e[n].invert = 0;
  9711. if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
  9712. e[n].invert = 1;
  9713. a=j,b=k;
  9714. }
  9715. e[n].x0 = p[a].x * scale_x + shift_x;
  9716. e[n].y0 = (p[a].y * y_scale_inv + shift_y) * (float)vsubsample;
  9717. e[n].x1 = p[b].x * scale_x + shift_x;
  9718. e[n].y1 = (p[b].y * y_scale_inv + shift_y) * (float)vsubsample;
  9719. ++n;
  9720. }
  9721. }
  9722. /* now sort the edges by their highest point (should snap to integer, and then by x) */
  9723. /*STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare); */
  9724. nk_tt__sort_edges(e, n);
  9725. /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
  9726. nk_tt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, alloc);
  9727. alloc->free(alloc->userdata, e);
  9728. }
  9729. NK_INTERN void
  9730. nk_tt__add_point(struct nk_tt__point *points, int n, float x, float y)
  9731. {
  9732. if (!points) return; /* during first pass, it's unallocated */
  9733. points[n].x = x;
  9734. points[n].y = y;
  9735. }
  9736. NK_INTERN int
  9737. nk_tt__tesselate_curve(struct nk_tt__point *points, int *num_points,
  9738. float x0, float y0, float x1, float y1, float x2, float y2,
  9739. float objspace_flatness_squared, int n)
  9740. {
  9741. /* tesselate until threshold p is happy...
  9742. * @TODO warped to compensate for non-linear stretching */
  9743. /* midpoint */
  9744. float mx = (x0 + 2*x1 + x2)/4;
  9745. float my = (y0 + 2*y1 + y2)/4;
  9746. /* versus directly drawn line */
  9747. float dx = (x0+x2)/2 - mx;
  9748. float dy = (y0+y2)/2 - my;
  9749. if (n > 16) /* 65536 segments on one curve better be enough! */
  9750. return 1;
  9751. /* half-pixel error allowed... need to be smaller if AA */
  9752. if (dx*dx+dy*dy > objspace_flatness_squared) {
  9753. nk_tt__tesselate_curve(points, num_points, x0,y0,
  9754. (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
  9755. nk_tt__tesselate_curve(points, num_points, mx,my,
  9756. (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
  9757. } else {
  9758. nk_tt__add_point(points, *num_points,x2,y2);
  9759. *num_points = *num_points+1;
  9760. }
  9761. return 1;
  9762. }
  9763. /* returns number of contours */
  9764. NK_INTERN struct nk_tt__point*
  9765. nk_tt_FlattenCurves(struct nk_tt_vertex *vertices, int num_verts,
  9766. float objspace_flatness, int **contour_lengths, int *num_contours,
  9767. struct nk_allocator *alloc)
  9768. {
  9769. struct nk_tt__point *points=0;
  9770. int num_points=0;
  9771. float objspace_flatness_squared = objspace_flatness * objspace_flatness;
  9772. int i;
  9773. int n=0;
  9774. int start=0;
  9775. int pass;
  9776. /* count how many "moves" there are to get the contour count */
  9777. for (i=0; i < num_verts; ++i)
  9778. if (vertices[i].type == NK_TT_vmove) ++n;
  9779. *num_contours = n;
  9780. if (n == 0) return 0;
  9781. *contour_lengths = (int *)
  9782. alloc->alloc(alloc->userdata,0, (sizeof(**contour_lengths) * (nk_size)n));
  9783. if (*contour_lengths == 0) {
  9784. *num_contours = 0;
  9785. return 0;
  9786. }
  9787. /* make two passes through the points so we don't need to realloc */
  9788. for (pass=0; pass < 2; ++pass)
  9789. {
  9790. float x=0,y=0;
  9791. if (pass == 1) {
  9792. points = (struct nk_tt__point *)
  9793. alloc->alloc(alloc->userdata,0, (nk_size)num_points * sizeof(points[0]));
  9794. if (points == 0) goto error;
  9795. }
  9796. num_points = 0;
  9797. n= -1;
  9798. for (i=0; i < num_verts; ++i)
  9799. {
  9800. switch (vertices[i].type) {
  9801. case NK_TT_vmove:
  9802. /* start the next contour */
  9803. if (n >= 0)
  9804. (*contour_lengths)[n] = num_points - start;
  9805. ++n;
  9806. start = num_points;
  9807. x = vertices[i].x, y = vertices[i].y;
  9808. nk_tt__add_point(points, num_points++, x,y);
  9809. break;
  9810. case NK_TT_vline:
  9811. x = vertices[i].x, y = vertices[i].y;
  9812. nk_tt__add_point(points, num_points++, x, y);
  9813. break;
  9814. case NK_TT_vcurve:
  9815. nk_tt__tesselate_curve(points, &num_points, x,y,
  9816. vertices[i].cx, vertices[i].cy,
  9817. vertices[i].x, vertices[i].y,
  9818. objspace_flatness_squared, 0);
  9819. x = vertices[i].x, y = vertices[i].y;
  9820. break;
  9821. default: break;
  9822. }
  9823. }
  9824. (*contour_lengths)[n] = num_points - start;
  9825. }
  9826. return points;
  9827. error:
  9828. alloc->free(alloc->userdata, points);
  9829. alloc->free(alloc->userdata, *contour_lengths);
  9830. *contour_lengths = 0;
  9831. *num_contours = 0;
  9832. return 0;
  9833. }
  9834. NK_INTERN void
  9835. nk_tt_Rasterize(struct nk_tt__bitmap *result, float flatness_in_pixels,
  9836. struct nk_tt_vertex *vertices, int num_verts,
  9837. float scale_x, float scale_y, float shift_x, float shift_y,
  9838. int x_off, int y_off, int invert, struct nk_allocator *alloc)
  9839. {
  9840. float scale = scale_x > scale_y ? scale_y : scale_x;
  9841. int winding_count, *winding_lengths;
  9842. struct nk_tt__point *windings = nk_tt_FlattenCurves(vertices, num_verts,
  9843. flatness_in_pixels / scale, &winding_lengths, &winding_count, alloc);
  9844. NK_ASSERT(alloc);
  9845. if (windings) {
  9846. nk_tt__rasterize(result, windings, winding_lengths, winding_count,
  9847. scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, alloc);
  9848. alloc->free(alloc->userdata, winding_lengths);
  9849. alloc->free(alloc->userdata, windings);
  9850. }
  9851. }
  9852. NK_INTERN void
  9853. nk_tt_MakeGlyphBitmapSubpixel(const struct nk_tt_fontinfo *info, unsigned char *output,
  9854. int out_w, int out_h, int out_stride, float scale_x, float scale_y,
  9855. float shift_x, float shift_y, int glyph, struct nk_allocator *alloc)
  9856. {
  9857. int ix0,iy0;
  9858. struct nk_tt_vertex *vertices;
  9859. int num_verts = nk_tt_GetGlyphShape(info, alloc, glyph, &vertices);
  9860. struct nk_tt__bitmap gbm;
  9861. nk_tt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x,
  9862. shift_y, &ix0,&iy0,0,0);
  9863. gbm.pixels = output;
  9864. gbm.w = out_w;
  9865. gbm.h = out_h;
  9866. gbm.stride = out_stride;
  9867. if (gbm.w && gbm.h)
  9868. nk_tt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y,
  9869. shift_x, shift_y, ix0,iy0, 1, alloc);
  9870. alloc->free(alloc->userdata, vertices);
  9871. }
  9872. /*-------------------------------------------------------------
  9873. * Bitmap baking
  9874. * --------------------------------------------------------------*/
  9875. NK_INTERN int
  9876. nk_tt_PackBegin(struct nk_tt_pack_context *spc, unsigned char *pixels,
  9877. int pw, int ph, int stride_in_bytes, int padding, struct nk_allocator *alloc)
  9878. {
  9879. int num_nodes = pw - padding;
  9880. struct nk_rp_context *context = (struct nk_rp_context *)
  9881. alloc->alloc(alloc->userdata,0, sizeof(*context));
  9882. struct nk_rp_node *nodes = (struct nk_rp_node*)
  9883. alloc->alloc(alloc->userdata,0, (sizeof(*nodes ) * (nk_size)num_nodes));
  9884. if (context == 0 || nodes == 0) {
  9885. if (context != 0) alloc->free(alloc->userdata, context);
  9886. if (nodes != 0) alloc->free(alloc->userdata, nodes);
  9887. return 0;
  9888. }
  9889. spc->width = pw;
  9890. spc->height = ph;
  9891. spc->pixels = pixels;
  9892. spc->pack_info = context;
  9893. spc->nodes = nodes;
  9894. spc->padding = padding;
  9895. spc->stride_in_bytes = (stride_in_bytes != 0) ? stride_in_bytes : pw;
  9896. spc->h_oversample = 1;
  9897. spc->v_oversample = 1;
  9898. nk_rp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
  9899. if (pixels)
  9900. NK_MEMSET(pixels, 0, (nk_size)(pw*ph)); /* background of 0 around pixels */
  9901. return 1;
  9902. }
  9903. NK_INTERN void
  9904. nk_tt_PackEnd(struct nk_tt_pack_context *spc, struct nk_allocator *alloc)
  9905. {
  9906. alloc->free(alloc->userdata, spc->nodes);
  9907. alloc->free(alloc->userdata, spc->pack_info);
  9908. }
  9909. NK_INTERN void
  9910. nk_tt_PackSetOversampling(struct nk_tt_pack_context *spc,
  9911. unsigned int h_oversample, unsigned int v_oversample)
  9912. {
  9913. NK_ASSERT(h_oversample <= NK_TT_MAX_OVERSAMPLE);
  9914. NK_ASSERT(v_oversample <= NK_TT_MAX_OVERSAMPLE);
  9915. if (h_oversample <= NK_TT_MAX_OVERSAMPLE)
  9916. spc->h_oversample = h_oversample;
  9917. if (v_oversample <= NK_TT_MAX_OVERSAMPLE)
  9918. spc->v_oversample = v_oversample;
  9919. }
  9920. NK_INTERN void
  9921. nk_tt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  9922. int kernel_width)
  9923. {
  9924. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  9925. int safe_w = w - kernel_width;
  9926. int j;
  9927. for (j=0; j < h; ++j)
  9928. {
  9929. int i;
  9930. unsigned int total;
  9931. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  9932. total = 0;
  9933. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  9934. switch (kernel_width) {
  9935. case 2:
  9936. for (i=0; i <= safe_w; ++i) {
  9937. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  9938. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  9939. pixels[i] = (unsigned char) (total / 2);
  9940. }
  9941. break;
  9942. case 3:
  9943. for (i=0; i <= safe_w; ++i) {
  9944. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  9945. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  9946. pixels[i] = (unsigned char) (total / 3);
  9947. }
  9948. break;
  9949. case 4:
  9950. for (i=0; i <= safe_w; ++i) {
  9951. total += (unsigned int)pixels[i] - buffer[i & NK_TT__OVER_MASK];
  9952. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  9953. pixels[i] = (unsigned char) (total / 4);
  9954. }
  9955. break;
  9956. case 5:
  9957. for (i=0; i <= safe_w; ++i) {
  9958. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  9959. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  9960. pixels[i] = (unsigned char) (total / 5);
  9961. }
  9962. break;
  9963. default:
  9964. for (i=0; i <= safe_w; ++i) {
  9965. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  9966. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  9967. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  9968. }
  9969. break;
  9970. }
  9971. for (; i < w; ++i) {
  9972. NK_ASSERT(pixels[i] == 0);
  9973. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  9974. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  9975. }
  9976. pixels += stride_in_bytes;
  9977. }
  9978. }
  9979. NK_INTERN void
  9980. nk_tt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  9981. int kernel_width)
  9982. {
  9983. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  9984. int safe_h = h - kernel_width;
  9985. int j;
  9986. for (j=0; j < w; ++j)
  9987. {
  9988. int i;
  9989. unsigned int total;
  9990. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  9991. total = 0;
  9992. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  9993. switch (kernel_width) {
  9994. case 2:
  9995. for (i=0; i <= safe_h; ++i) {
  9996. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  9997. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  9998. pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
  9999. }
  10000. break;
  10001. case 3:
  10002. for (i=0; i <= safe_h; ++i) {
  10003. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  10004. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  10005. pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
  10006. }
  10007. break;
  10008. case 4:
  10009. for (i=0; i <= safe_h; ++i) {
  10010. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  10011. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  10012. pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
  10013. }
  10014. break;
  10015. case 5:
  10016. for (i=0; i <= safe_h; ++i) {
  10017. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  10018. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  10019. pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
  10020. }
  10021. break;
  10022. default:
  10023. for (i=0; i <= safe_h; ++i) {
  10024. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  10025. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  10026. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  10027. }
  10028. break;
  10029. }
  10030. for (; i < h; ++i) {
  10031. NK_ASSERT(pixels[i*stride_in_bytes] == 0);
  10032. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  10033. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  10034. }
  10035. pixels += 1;
  10036. }
  10037. }
  10038. NK_INTERN float
  10039. nk_tt__oversample_shift(int oversample)
  10040. {
  10041. if (!oversample)
  10042. return 0.0f;
  10043. /* The prefilter is a box filter of width "oversample", */
  10044. /* which shifts phase by (oversample - 1)/2 pixels in */
  10045. /* oversampled space. We want to shift in the opposite */
  10046. /* direction to counter this. */
  10047. return (float)-(oversample - 1) / (2.0f * (float)oversample);
  10048. }
  10049. /* rects array must be big enough to accommodate all characters in the given ranges */
  10050. NK_INTERN int
  10051. nk_tt_PackFontRangesGatherRects(struct nk_tt_pack_context *spc,
  10052. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  10053. int num_ranges, struct nk_rp_rect *rects)
  10054. {
  10055. int i,j,k;
  10056. k = 0;
  10057. for (i=0; i < num_ranges; ++i) {
  10058. float fh = ranges[i].font_size;
  10059. float scale = (fh > 0) ? nk_tt_ScaleForPixelHeight(info, fh):
  10060. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  10061. ranges[i].h_oversample = (unsigned char) spc->h_oversample;
  10062. ranges[i].v_oversample = (unsigned char) spc->v_oversample;
  10063. for (j=0; j < ranges[i].num_chars; ++j) {
  10064. int x0,y0,x1,y1;
  10065. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  10066. ranges[i].first_unicode_codepoint_in_range + j :
  10067. ranges[i].array_of_unicode_codepoints[j];
  10068. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  10069. nk_tt_GetGlyphBitmapBoxSubpixel(info,glyph, scale * (float)spc->h_oversample,
  10070. scale * (float)spc->v_oversample, 0,0, &x0,&y0,&x1,&y1);
  10071. rects[k].w = (nk_rp_coord) (x1-x0 + spc->padding + (int)spc->h_oversample-1);
  10072. rects[k].h = (nk_rp_coord) (y1-y0 + spc->padding + (int)spc->v_oversample-1);
  10073. ++k;
  10074. }
  10075. }
  10076. return k;
  10077. }
  10078. NK_INTERN int
  10079. nk_tt_PackFontRangesRenderIntoRects(struct nk_tt_pack_context *spc,
  10080. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  10081. int num_ranges, struct nk_rp_rect *rects, struct nk_allocator *alloc)
  10082. {
  10083. int i,j,k, return_value = 1;
  10084. /* save current values */
  10085. int old_h_over = (int)spc->h_oversample;
  10086. int old_v_over = (int)spc->v_oversample;
  10087. /* rects array must be big enough to accommodate all characters in the given ranges */
  10088. k = 0;
  10089. for (i=0; i < num_ranges; ++i)
  10090. {
  10091. float fh = ranges[i].font_size;
  10092. float recip_h,recip_v,sub_x,sub_y;
  10093. float scale = fh > 0 ? nk_tt_ScaleForPixelHeight(info, fh):
  10094. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  10095. spc->h_oversample = ranges[i].h_oversample;
  10096. spc->v_oversample = ranges[i].v_oversample;
  10097. recip_h = 1.0f / (float)spc->h_oversample;
  10098. recip_v = 1.0f / (float)spc->v_oversample;
  10099. sub_x = nk_tt__oversample_shift((int)spc->h_oversample);
  10100. sub_y = nk_tt__oversample_shift((int)spc->v_oversample);
  10101. for (j=0; j < ranges[i].num_chars; ++j)
  10102. {
  10103. struct nk_rp_rect *r = &rects[k];
  10104. if (r->was_packed)
  10105. {
  10106. struct nk_tt_packedchar *bc = &ranges[i].chardata_for_range[j];
  10107. int advance, lsb, x0,y0,x1,y1;
  10108. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  10109. ranges[i].first_unicode_codepoint_in_range + j :
  10110. ranges[i].array_of_unicode_codepoints[j];
  10111. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  10112. nk_rp_coord pad = (nk_rp_coord) spc->padding;
  10113. /* pad on left and top */
  10114. r->x = (nk_rp_coord)((int)r->x + (int)pad);
  10115. r->y = (nk_rp_coord)((int)r->y + (int)pad);
  10116. r->w = (nk_rp_coord)((int)r->w - (int)pad);
  10117. r->h = (nk_rp_coord)((int)r->h - (int)pad);
  10118. nk_tt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
  10119. nk_tt_GetGlyphBitmapBox(info, glyph, scale * (float)spc->h_oversample,
  10120. (scale * (float)spc->v_oversample), &x0,&y0,&x1,&y1);
  10121. nk_tt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y*spc->stride_in_bytes,
  10122. (int)(r->w - spc->h_oversample+1), (int)(r->h - spc->v_oversample+1),
  10123. spc->stride_in_bytes, scale * (float)spc->h_oversample,
  10124. scale * (float)spc->v_oversample, 0,0, glyph, alloc);
  10125. if (spc->h_oversample > 1)
  10126. nk_tt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  10127. r->w, r->h, spc->stride_in_bytes, (int)spc->h_oversample);
  10128. if (spc->v_oversample > 1)
  10129. nk_tt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  10130. r->w, r->h, spc->stride_in_bytes, (int)spc->v_oversample);
  10131. bc->x0 = (nk_ushort) r->x;
  10132. bc->y0 = (nk_ushort) r->y;
  10133. bc->x1 = (nk_ushort) (r->x + r->w);
  10134. bc->y1 = (nk_ushort) (r->y + r->h);
  10135. bc->xadvance = scale * (float)advance;
  10136. bc->xoff = (float) x0 * recip_h + sub_x;
  10137. bc->yoff = (float) y0 * recip_v + sub_y;
  10138. bc->xoff2 = ((float)x0 + r->w) * recip_h + sub_x;
  10139. bc->yoff2 = ((float)y0 + r->h) * recip_v + sub_y;
  10140. } else {
  10141. return_value = 0; /* if any fail, report failure */
  10142. }
  10143. ++k;
  10144. }
  10145. }
  10146. /* restore original values */
  10147. spc->h_oversample = (unsigned int)old_h_over;
  10148. spc->v_oversample = (unsigned int)old_v_over;
  10149. return return_value;
  10150. }
  10151. NK_INTERN void
  10152. nk_tt_GetPackedQuad(struct nk_tt_packedchar *chardata, int pw, int ph,
  10153. int char_index, float *xpos, float *ypos, struct nk_tt_aligned_quad *q,
  10154. int align_to_integer)
  10155. {
  10156. float ipw = 1.0f / (float)pw, iph = 1.0f / (float)ph;
  10157. struct nk_tt_packedchar *b = (struct nk_tt_packedchar*)(chardata + char_index);
  10158. if (align_to_integer) {
  10159. int tx = nk_ifloorf((*xpos + b->xoff) + 0.5f);
  10160. int ty = nk_ifloorf((*ypos + b->yoff) + 0.5f);
  10161. float x = (float)tx;
  10162. float y = (float)ty;
  10163. q->x0 = x;
  10164. q->y0 = y;
  10165. q->x1 = x + b->xoff2 - b->xoff;
  10166. q->y1 = y + b->yoff2 - b->yoff;
  10167. } else {
  10168. q->x0 = *xpos + b->xoff;
  10169. q->y0 = *ypos + b->yoff;
  10170. q->x1 = *xpos + b->xoff2;
  10171. q->y1 = *ypos + b->yoff2;
  10172. }
  10173. q->s0 = b->x0 * ipw;
  10174. q->t0 = b->y0 * iph;
  10175. q->s1 = b->x1 * ipw;
  10176. q->t1 = b->y1 * iph;
  10177. *xpos += b->xadvance;
  10178. }
  10179. /* -------------------------------------------------------------
  10180. *
  10181. * FONT BAKING
  10182. *
  10183. * --------------------------------------------------------------*/
  10184. struct nk_font_bake_data {
  10185. struct nk_tt_fontinfo info;
  10186. struct nk_rp_rect *rects;
  10187. struct nk_tt_pack_range *ranges;
  10188. nk_rune range_count;
  10189. };
  10190. struct nk_font_baker {
  10191. struct nk_allocator alloc;
  10192. struct nk_tt_pack_context spc;
  10193. struct nk_font_bake_data *build;
  10194. struct nk_tt_packedchar *packed_chars;
  10195. struct nk_rp_rect *rects;
  10196. struct nk_tt_pack_range *ranges;
  10197. };
  10198. NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct nk_rp_rect);
  10199. NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(struct nk_tt_pack_range);
  10200. NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(struct nk_tt_packedchar);
  10201. NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
  10202. NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
  10203. NK_INTERN int
  10204. nk_range_count(const nk_rune *range)
  10205. {
  10206. const nk_rune *iter = range;
  10207. NK_ASSERT(range);
  10208. if (!range) return 0;
  10209. while (*(iter++) != 0);
  10210. return (iter == range) ? 0 : (int)((iter - range)/2);
  10211. }
  10212. NK_INTERN int
  10213. nk_range_glyph_count(const nk_rune *range, int count)
  10214. {
  10215. int i = 0;
  10216. int total_glyphs = 0;
  10217. for (i = 0; i < count; ++i) {
  10218. int diff;
  10219. nk_rune f = range[(i*2)+0];
  10220. nk_rune t = range[(i*2)+1];
  10221. NK_ASSERT(t >= f);
  10222. diff = (int)((t - f) + 1);
  10223. total_glyphs += diff;
  10224. }
  10225. return total_glyphs;
  10226. }
  10227. NK_API const nk_rune*
  10228. nk_font_default_glyph_ranges(void)
  10229. {
  10230. NK_STORAGE const nk_rune ranges[] = {0x0020, 0x00FF, 0};
  10231. return ranges;
  10232. }
  10233. NK_API const nk_rune*
  10234. nk_font_chinese_glyph_ranges(void)
  10235. {
  10236. NK_STORAGE const nk_rune ranges[] = {
  10237. 0x0020, 0x00FF,
  10238. 0x3000, 0x30FF,
  10239. 0x31F0, 0x31FF,
  10240. 0xFF00, 0xFFEF,
  10241. 0x4e00, 0x9FAF,
  10242. 0
  10243. };
  10244. return ranges;
  10245. }
  10246. NK_API const nk_rune*
  10247. nk_font_cyrillic_glyph_ranges(void)
  10248. {
  10249. NK_STORAGE const nk_rune ranges[] = {
  10250. 0x0020, 0x00FF,
  10251. 0x0400, 0x052F,
  10252. 0x2DE0, 0x2DFF,
  10253. 0xA640, 0xA69F,
  10254. 0
  10255. };
  10256. return ranges;
  10257. }
  10258. NK_API const nk_rune*
  10259. nk_font_korean_glyph_ranges(void)
  10260. {
  10261. NK_STORAGE const nk_rune ranges[] = {
  10262. 0x0020, 0x00FF,
  10263. 0x3131, 0x3163,
  10264. 0xAC00, 0xD79D,
  10265. 0
  10266. };
  10267. return ranges;
  10268. }
  10269. NK_INTERN void
  10270. nk_font_baker_memory(nk_size *temp, int *glyph_count,
  10271. struct nk_font_config *config_list, int count)
  10272. {
  10273. int range_count = 0;
  10274. int total_range_count = 0;
  10275. struct nk_font_config *iter;
  10276. NK_ASSERT(config_list);
  10277. NK_ASSERT(glyph_count);
  10278. if (!config_list) {
  10279. *temp = 0;
  10280. *glyph_count = 0;
  10281. return;
  10282. }
  10283. *glyph_count = 0;
  10284. if (!config_list->range)
  10285. config_list->range = nk_font_default_glyph_ranges();
  10286. for (iter = config_list; iter; iter = iter->next) {
  10287. range_count = nk_range_count(iter->range);
  10288. total_range_count += range_count;
  10289. *glyph_count += nk_range_glyph_count(iter->range, range_count);
  10290. }
  10291. *temp = (nk_size)*glyph_count * sizeof(struct nk_rp_rect);
  10292. *temp += (nk_size)total_range_count * sizeof(struct nk_tt_pack_range);
  10293. *temp += (nk_size)*glyph_count * sizeof(struct nk_tt_packedchar);
  10294. *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
  10295. *temp += sizeof(struct nk_font_baker);
  10296. *temp += nk_rect_align + nk_range_align + nk_char_align;
  10297. *temp += nk_build_align + nk_baker_align;
  10298. }
  10299. NK_INTERN struct nk_font_baker*
  10300. nk_font_baker(void *memory, int glyph_count, int count, struct nk_allocator *alloc)
  10301. {
  10302. struct nk_font_baker *baker;
  10303. if (!memory) return 0;
  10304. /* setup baker inside a memory block */
  10305. baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
  10306. baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
  10307. baker->packed_chars = (struct nk_tt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
  10308. baker->rects = (struct nk_rp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
  10309. baker->ranges = (struct nk_tt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
  10310. baker->alloc = *alloc;
  10311. return baker;
  10312. }
  10313. NK_INTERN int
  10314. nk_font_bake_pack(struct nk_font_baker *baker,
  10315. nk_size *image_memory, int *width, int *height, struct nk_recti *custom,
  10316. const struct nk_font_config *config_list, int count,
  10317. struct nk_allocator *alloc)
  10318. {
  10319. NK_STORAGE const nk_size max_height = 1024 * 32;
  10320. const struct nk_font_config *config_iter;
  10321. int total_glyph_count = 0;
  10322. int total_range_count = 0;
  10323. int range_count = 0;
  10324. int i = 0;
  10325. NK_ASSERT(image_memory);
  10326. NK_ASSERT(width);
  10327. NK_ASSERT(height);
  10328. NK_ASSERT(config_list);
  10329. NK_ASSERT(count);
  10330. NK_ASSERT(alloc);
  10331. if (!image_memory || !width || !height || !config_list || !count) return nk_false;
  10332. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  10333. range_count = nk_range_count(config_iter->range);
  10334. total_range_count += range_count;
  10335. total_glyph_count += nk_range_glyph_count(config_iter->range, range_count);
  10336. }
  10337. /* setup font baker from temporary memory */
  10338. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  10339. const struct nk_font_config *cfg = config_iter;
  10340. if (!nk_tt_InitFont(&baker->build[i++].info, (const unsigned char*)cfg->ttf_blob, 0))
  10341. return nk_false;
  10342. }
  10343. *height = 0;
  10344. *width = (total_glyph_count > 1000) ? 1024 : 512;
  10345. nk_tt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, alloc);
  10346. {
  10347. int input_i = 0;
  10348. int range_n = 0;
  10349. int rect_n = 0;
  10350. int char_n = 0;
  10351. if (custom) {
  10352. /* pack custom user data first so it will be in the upper left corner*/
  10353. struct nk_rp_rect custom_space;
  10354. nk_zero(&custom_space, sizeof(custom_space));
  10355. custom_space.w = (nk_rp_coord)((custom->w * 2) + 1);
  10356. custom_space.h = (nk_rp_coord)(custom->h + 1);
  10357. nk_tt_PackSetOversampling(&baker->spc, 1, 1);
  10358. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, &custom_space, 1);
  10359. *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
  10360. custom->x = (short)custom_space.x;
  10361. custom->y = (short)custom_space.y;
  10362. custom->w = (short)custom_space.w;
  10363. custom->h = (short)custom_space.h;
  10364. }
  10365. /* first font pass: pack all glyphs */
  10366. for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
  10367. input_i++, config_iter = config_iter->next)
  10368. {
  10369. int n = 0;
  10370. int glyph_count;
  10371. const nk_rune *in_range;
  10372. const struct nk_font_config *cfg = config_iter;
  10373. struct nk_font_bake_data *tmp = &baker->build[input_i];
  10374. /* count glyphs + ranges in current font */
  10375. glyph_count = 0; range_count = 0;
  10376. for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
  10377. glyph_count += (int)(in_range[1] - in_range[0]) + 1;
  10378. range_count++;
  10379. }
  10380. /* setup ranges */
  10381. tmp->ranges = baker->ranges + range_n;
  10382. tmp->range_count = (nk_rune)range_count;
  10383. range_n += range_count;
  10384. for (i = 0; i < range_count; ++i) {
  10385. in_range = &cfg->range[i * 2];
  10386. tmp->ranges[i].font_size = cfg->size;
  10387. tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
  10388. tmp->ranges[i].num_chars = (int)(in_range[1]- in_range[0]) + 1;
  10389. tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
  10390. char_n += tmp->ranges[i].num_chars;
  10391. }
  10392. /* pack */
  10393. tmp->rects = baker->rects + rect_n;
  10394. rect_n += glyph_count;
  10395. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  10396. n = nk_tt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
  10397. tmp->ranges, (int)tmp->range_count, tmp->rects);
  10398. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, tmp->rects, (int)n);
  10399. /* texture height */
  10400. for (i = 0; i < n; ++i) {
  10401. if (tmp->rects[i].was_packed)
  10402. *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
  10403. }
  10404. }
  10405. NK_ASSERT(rect_n == total_glyph_count);
  10406. NK_ASSERT(char_n == total_glyph_count);
  10407. NK_ASSERT(range_n == total_range_count);
  10408. }
  10409. *height = (int)nk_round_up_pow2((nk_uint)*height);
  10410. *image_memory = (nk_size)(*width) * (nk_size)(*height);
  10411. return nk_true;
  10412. }
  10413. NK_INTERN void
  10414. nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int height,
  10415. struct nk_font_glyph *glyphs, int glyphs_count,
  10416. const struct nk_font_config *config_list, int font_count)
  10417. {
  10418. int input_i = 0;
  10419. nk_rune glyph_n = 0;
  10420. const struct nk_font_config *config_iter;
  10421. NK_ASSERT(image_memory);
  10422. NK_ASSERT(width);
  10423. NK_ASSERT(height);
  10424. NK_ASSERT(config_list);
  10425. NK_ASSERT(baker);
  10426. NK_ASSERT(font_count);
  10427. NK_ASSERT(glyphs_count);
  10428. if (!image_memory || !width || !height || !config_list ||
  10429. !font_count || !glyphs || !glyphs_count)
  10430. return;
  10431. /* second font pass: render glyphs */
  10432. nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
  10433. baker->spc.pixels = (unsigned char*)image_memory;
  10434. baker->spc.height = (int)height;
  10435. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  10436. ++input_i, config_iter = config_iter->next)
  10437. {
  10438. const struct nk_font_config *cfg = config_iter;
  10439. struct nk_font_bake_data *tmp = &baker->build[input_i];
  10440. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  10441. nk_tt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges,
  10442. (int)tmp->range_count, tmp->rects, &baker->alloc);
  10443. }
  10444. nk_tt_PackEnd(&baker->spc, &baker->alloc);
  10445. /* third pass: setup font and glyphs */
  10446. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  10447. ++input_i, config_iter = config_iter->next)
  10448. {
  10449. nk_size i = 0;
  10450. int char_idx = 0;
  10451. nk_rune glyph_count = 0;
  10452. const struct nk_font_config *cfg = config_iter;
  10453. struct nk_font_bake_data *tmp = &baker->build[input_i];
  10454. struct nk_baked_font *dst_font = cfg->font;
  10455. float font_scale = nk_tt_ScaleForPixelHeight(&tmp->info, cfg->size);
  10456. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  10457. nk_tt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
  10458. &unscaled_line_gap);
  10459. /* fill baked font */
  10460. if (!cfg->merge_mode) {
  10461. dst_font->ranges = cfg->range;
  10462. dst_font->height = cfg->size;
  10463. dst_font->ascent = ((float)unscaled_ascent * font_scale);
  10464. dst_font->descent = ((float)unscaled_descent * font_scale);
  10465. dst_font->glyph_offset = glyph_n;
  10466. }
  10467. /* fill own baked font glyph array */
  10468. for (i = 0; i < tmp->range_count; ++i)
  10469. {
  10470. struct nk_tt_pack_range *range = &tmp->ranges[i];
  10471. for (char_idx = 0; char_idx < range->num_chars; char_idx++)
  10472. {
  10473. nk_rune codepoint = 0;
  10474. float dummy_x = 0, dummy_y = 0;
  10475. struct nk_tt_aligned_quad q;
  10476. struct nk_font_glyph *glyph;
  10477. /* query glyph bounds from stb_truetype */
  10478. const struct nk_tt_packedchar *pc = &range->chardata_for_range[char_idx];
  10479. if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
  10480. codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
  10481. nk_tt_GetPackedQuad(range->chardata_for_range, (int)width,
  10482. (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
  10483. /* fill own glyph type with data */
  10484. glyph = &glyphs[dst_font->glyph_offset + (unsigned int)glyph_count];
  10485. glyph->codepoint = codepoint;
  10486. glyph->x0 = q.x0; glyph->y0 = q.y0;
  10487. glyph->x1 = q.x1; glyph->y1 = q.y1;
  10488. glyph->y0 += (dst_font->ascent + 0.5f);
  10489. glyph->y1 += (dst_font->ascent + 0.5f);
  10490. glyph->w = glyph->x1 - glyph->x0 + 0.5f;
  10491. glyph->h = glyph->y1 - glyph->y0;
  10492. if (cfg->coord_type == NK_COORD_PIXEL) {
  10493. glyph->u0 = q.s0 * (float)width;
  10494. glyph->v0 = q.t0 * (float)height;
  10495. glyph->u1 = q.s1 * (float)width;
  10496. glyph->v1 = q.t1 * (float)height;
  10497. } else {
  10498. glyph->u0 = q.s0;
  10499. glyph->v0 = q.t0;
  10500. glyph->u1 = q.s1;
  10501. glyph->v1 = q.t1;
  10502. }
  10503. glyph->xadvance = (pc->xadvance + cfg->spacing.x);
  10504. if (cfg->pixel_snap)
  10505. glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
  10506. glyph_count++;
  10507. }
  10508. }
  10509. dst_font->glyph_count = glyph_count;
  10510. glyph_n += dst_font->glyph_count;
  10511. }
  10512. }
  10513. NK_INTERN void
  10514. nk_font_bake_custom_data(void *img_memory, int img_width, int img_height,
  10515. struct nk_recti img_dst, const char *texture_data_mask, int tex_width,
  10516. int tex_height, char white, char black)
  10517. {
  10518. nk_byte *pixels;
  10519. int y = 0;
  10520. int x = 0;
  10521. int n = 0;
  10522. NK_ASSERT(img_memory);
  10523. NK_ASSERT(img_width);
  10524. NK_ASSERT(img_height);
  10525. NK_ASSERT(texture_data_mask);
  10526. NK_UNUSED(tex_height);
  10527. if (!img_memory || !img_width || !img_height || !texture_data_mask)
  10528. return;
  10529. pixels = (nk_byte*)img_memory;
  10530. for (y = 0, n = 0; y < tex_height; ++y) {
  10531. for (x = 0; x < tex_width; ++x, ++n) {
  10532. const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
  10533. const int off1 = off0 + 1 + tex_width;
  10534. pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
  10535. pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
  10536. }
  10537. }
  10538. }
  10539. NK_INTERN void
  10540. nk_font_bake_convert(void *out_memory, int img_width, int img_height,
  10541. const void *in_memory)
  10542. {
  10543. int n = 0;
  10544. nk_rune *dst;
  10545. const nk_byte *src;
  10546. NK_ASSERT(out_memory);
  10547. NK_ASSERT(in_memory);
  10548. NK_ASSERT(img_width);
  10549. NK_ASSERT(img_height);
  10550. if (!out_memory || !in_memory || !img_height || !img_width) return;
  10551. dst = (nk_rune*)out_memory;
  10552. src = (const nk_byte*)in_memory;
  10553. for (n = (int)(img_width * img_height); n > 0; n--)
  10554. *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
  10555. }
  10556. /* -------------------------------------------------------------
  10557. *
  10558. * FONT
  10559. *
  10560. * --------------------------------------------------------------*/
  10561. NK_INTERN float
  10562. nk_font_text_width(nk_handle handle, float height, const char *text, int len)
  10563. {
  10564. nk_rune unicode;
  10565. int text_len = 0;
  10566. float text_width = 0;
  10567. int glyph_len = 0;
  10568. float scale = 0;
  10569. struct nk_font *font = (struct nk_font*)handle.ptr;
  10570. NK_ASSERT(font);
  10571. NK_ASSERT(font->glyphs);
  10572. if (!font || !text || !len)
  10573. return 0;
  10574. scale = height/font->info.height;
  10575. glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
  10576. if (!glyph_len) return 0;
  10577. while (text_len <= (int)len && glyph_len) {
  10578. const struct nk_font_glyph *g;
  10579. if (unicode == NK_UTF_INVALID) break;
  10580. /* query currently drawn glyph information */
  10581. g = nk_font_find_glyph(font, unicode);
  10582. text_width += g->xadvance * scale;
  10583. /* offset next glyph */
  10584. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
  10585. text_len += glyph_len;
  10586. }
  10587. return text_width;
  10588. }
  10589. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  10590. NK_INTERN void
  10591. nk_font_query_font_glyph(nk_handle handle, float height,
  10592. struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  10593. {
  10594. float scale;
  10595. const struct nk_font_glyph *g;
  10596. struct nk_font *font;
  10597. NK_ASSERT(glyph);
  10598. NK_UNUSED(next_codepoint);
  10599. font = (struct nk_font*)handle.ptr;
  10600. NK_ASSERT(font);
  10601. NK_ASSERT(font->glyphs);
  10602. if (!font || !glyph)
  10603. return;
  10604. scale = height/font->info.height;
  10605. g = nk_font_find_glyph(font, codepoint);
  10606. glyph->width = (g->x1 - g->x0) * scale;
  10607. glyph->height = (g->y1 - g->y0) * scale;
  10608. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  10609. glyph->xadvance = (g->xadvance * scale);
  10610. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  10611. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  10612. }
  10613. #endif
  10614. NK_API const struct nk_font_glyph*
  10615. nk_font_find_glyph(struct nk_font *font, nk_rune unicode)
  10616. {
  10617. int i = 0;
  10618. int count;
  10619. int total_glyphs = 0;
  10620. const struct nk_font_glyph *glyph = 0;
  10621. NK_ASSERT(font);
  10622. NK_ASSERT(font->glyphs);
  10623. NK_ASSERT(font->info.ranges);
  10624. if (!font || !font->glyphs) return 0;
  10625. glyph = font->fallback;
  10626. count = nk_range_count(font->info.ranges);
  10627. for (i = 0; i < count; ++i) {
  10628. nk_rune f = font->info.ranges[(i*2)+0];
  10629. nk_rune t = font->info.ranges[(i*2)+1];
  10630. int diff = (int)((t - f) + 1);
  10631. if (unicode >= f && unicode <= t)
  10632. return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
  10633. total_glyphs += diff;
  10634. }
  10635. return glyph;
  10636. }
  10637. NK_INTERN void
  10638. nk_font_init(struct nk_font *font, float pixel_height,
  10639. nk_rune fallback_codepoint, struct nk_font_glyph *glyphs,
  10640. const struct nk_baked_font *baked_font, nk_handle atlas)
  10641. {
  10642. struct nk_baked_font baked;
  10643. NK_ASSERT(font);
  10644. NK_ASSERT(glyphs);
  10645. NK_ASSERT(baked_font);
  10646. if (!font || !glyphs || !baked_font)
  10647. return;
  10648. baked = *baked_font;
  10649. font->fallback = 0;
  10650. font->info = baked;
  10651. font->scale = (float)pixel_height / (float)font->info.height;
  10652. font->glyphs = &glyphs[baked_font->glyph_offset];
  10653. font->texture = atlas;
  10654. font->fallback_codepoint = fallback_codepoint;
  10655. font->fallback = nk_font_find_glyph(font, fallback_codepoint);
  10656. font->handle.height = font->info.height * font->scale;
  10657. font->handle.width = nk_font_text_width;
  10658. font->handle.userdata.ptr = font;
  10659. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  10660. font->handle.query = nk_font_query_font_glyph;
  10661. font->handle.texture = font->texture;
  10662. #endif
  10663. }
  10664. /* ---------------------------------------------------------------------------
  10665. *
  10666. * DEFAULT FONT
  10667. *
  10668. * ProggyClean.ttf
  10669. * Copyright (c) 2004, 2005 Tristan Grimmer
  10670. * MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
  10671. * Download and more information at http://upperbounds.net
  10672. *-----------------------------------------------------------------------------*/
  10673. #ifdef NK_INCLUDE_DEFAULT_FONT
  10674. #ifdef __clang__
  10675. #pragma clang diagnostic push
  10676. #pragma clang diagnostic ignored "-Woverlength-strings"
  10677. #elif defined(__GNUC__) || defined(__GNUG__)
  10678. #pragma GCC diagnostic push
  10679. #pragma GCC diagnostic ignored "-Woverlength-strings"
  10680. #endif
  10681. NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980+1] =
  10682. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  10683. "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#"
  10684. "`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"
  10685. "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"
  10686. "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"
  10687. "*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)"
  10688. "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"
  10689. "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."
  10690. "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"
  10691. "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(,)"
  10692. "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#"
  10693. "'/###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"
  10694. "_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"
  10695. "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/"
  10696. "/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"
  10697. "%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&#"
  10698. "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)("
  10699. "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"
  10700. "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"
  10701. "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^-"
  10702. "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-"
  10703. "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"
  10704. "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%"
  10705. "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]"
  10706. "%(?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"
  10707. "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:"
  10708. "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("
  10709. "$/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<"
  10710. "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'?"
  10711. "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/;"
  10712. ")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"
  10713. "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("
  10714. "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"
  10715. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  10716. "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<-"
  10717. "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"
  10718. "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"
  10719. ".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@@"
  10720. "$&)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*"
  10721. "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"
  10722. "@-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&#"
  10723. "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#"
  10724. "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"
  10725. "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"
  10726. "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&#"
  10727. "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"
  10728. ":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+"
  10729. "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*"
  10730. "$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"
  10731. ":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"
  10732. "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"
  10733. "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"
  10734. "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"
  10735. ":^#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@&]>"
  10736. "_>@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%"
  10737. "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;"
  10738. "^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:"
  10739. "+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%"
  10740. "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-"
  10741. "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*"
  10742. "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"
  10743. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  10744. "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`"
  10745. "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]%/"
  10746. "+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"
  10747. "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"
  10748. "?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"
  10749. "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"
  10750. ">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'>"
  10751. "[%$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"
  10752. "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#"
  10753. "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$"
  10754. "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,)"
  10755. "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"
  10756. "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"
  10757. "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"
  10758. "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(#"
  10759. ";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&>"
  10760. "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#"
  10761. "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"
  10762. "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#"
  10763. "/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-#"
  10764. "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#"
  10765. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  10766. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  10767. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  10768. #endif /* NK_INCLUDE_DEFAULT_FONT */
  10769. #define NK_CURSOR_DATA_W 90
  10770. #define NK_CURSOR_DATA_H 27
  10771. NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
  10772. {
  10773. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  10774. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  10775. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  10776. "X - X.X - X.....X - X.....X -X...X - X...X"
  10777. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  10778. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  10779. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  10780. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  10781. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  10782. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  10783. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  10784. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  10785. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  10786. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  10787. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  10788. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  10789. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  10790. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  10791. "X.X X..X - -X.......X- X.......X - XX XX - "
  10792. "XX X..X - - X.....X - X.....X - X.X X.X - "
  10793. " X..X - X...X - X...X - X..X X..X - "
  10794. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  10795. "------------ - X - X -X.....................X- "
  10796. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  10797. " - X..X X..X - "
  10798. " - X.X X.X - "
  10799. " - XX XX - "
  10800. };
  10801. #ifdef __clang__
  10802. #pragma clang diagnostic pop
  10803. #elif defined(__GNUC__) || defined(__GNUG__)
  10804. #pragma GCC diagnostic pop
  10805. #endif
  10806. NK_INTERN unsigned int
  10807. nk_decompress_length(unsigned char *input)
  10808. {
  10809. return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
  10810. }
  10811. NK_GLOBAL unsigned char *nk__barrier;
  10812. NK_GLOBAL unsigned char *nk__barrier2;
  10813. NK_GLOBAL unsigned char *nk__barrier3;
  10814. NK_GLOBAL unsigned char *nk__barrier4;
  10815. NK_GLOBAL unsigned char *nk__dout;
  10816. NK_INTERN void
  10817. nk__match(unsigned char *data, unsigned int length)
  10818. {
  10819. /* INVERSE of memmove... write each byte before copying the next...*/
  10820. NK_ASSERT (nk__dout + length <= nk__barrier);
  10821. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  10822. if (data < nk__barrier4) { nk__dout = nk__barrier+1; return; }
  10823. while (length--) *nk__dout++ = *data++;
  10824. }
  10825. NK_INTERN void
  10826. nk__lit(unsigned char *data, unsigned int length)
  10827. {
  10828. NK_ASSERT (nk__dout + length <= nk__barrier);
  10829. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  10830. if (data < nk__barrier2) { nk__dout = nk__barrier+1; return; }
  10831. NK_MEMCPY(nk__dout, data, length);
  10832. nk__dout += length;
  10833. }
  10834. #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
  10835. #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
  10836. #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
  10837. NK_INTERN unsigned char*
  10838. nk_decompress_token(unsigned char *i)
  10839. {
  10840. if (*i >= 0x20) { /* use fewer if's for cases that expand small */
  10841. if (*i >= 0x80) nk__match(nk__dout-i[1]-1, (unsigned int)i[0] - 0x80 + 1), i += 2;
  10842. else if (*i >= 0x40) nk__match(nk__dout-(nk__in2(0) - 0x4000 + 1), (unsigned int)i[2]+1), i += 3;
  10843. else /* *i >= 0x20 */ nk__lit(i+1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  10844. } else { /* more ifs for cases that expand large, since overhead is amortized */
  10845. if (*i >= 0x18) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3]+1), i += 4;
  10846. else if (*i >= 0x10) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3)+1), i += 5;
  10847. else if (*i >= 0x08) nk__lit(i+2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
  10848. else if (*i == 0x07) nk__lit(i+3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
  10849. else if (*i == 0x06) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), i[4]+1u), i += 5;
  10850. else if (*i == 0x04) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), (unsigned int)nk__in2(4)+1u), i += 6;
  10851. }
  10852. return i;
  10853. }
  10854. NK_INTERN unsigned int
  10855. nk_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
  10856. {
  10857. const unsigned long ADLER_MOD = 65521;
  10858. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  10859. unsigned long blocklen, i;
  10860. blocklen = buflen % 5552;
  10861. while (buflen) {
  10862. for (i=0; i + 7 < blocklen; i += 8) {
  10863. s1 += buffer[0]; s2 += s1;
  10864. s1 += buffer[1]; s2 += s1;
  10865. s1 += buffer[2]; s2 += s1;
  10866. s1 += buffer[3]; s2 += s1;
  10867. s1 += buffer[4]; s2 += s1;
  10868. s1 += buffer[5]; s2 += s1;
  10869. s1 += buffer[6]; s2 += s1;
  10870. s1 += buffer[7]; s2 += s1;
  10871. buffer += 8;
  10872. }
  10873. for (; i < blocklen; ++i) {
  10874. s1 += *buffer++; s2 += s1;
  10875. }
  10876. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  10877. buflen -= (unsigned int)blocklen;
  10878. blocklen = 5552;
  10879. }
  10880. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  10881. }
  10882. NK_INTERN unsigned int
  10883. nk_decompress(unsigned char *output, unsigned char *i, unsigned int length)
  10884. {
  10885. unsigned int olen;
  10886. if (nk__in4(0) != 0x57bC0000) return 0;
  10887. if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
  10888. olen = nk_decompress_length(i);
  10889. nk__barrier2 = i;
  10890. nk__barrier3 = i+length;
  10891. nk__barrier = output + olen;
  10892. nk__barrier4 = output;
  10893. i += 16;
  10894. nk__dout = output;
  10895. for (;;) {
  10896. unsigned char *old_i = i;
  10897. i = nk_decompress_token(i);
  10898. if (i == old_i) {
  10899. if (*i == 0x05 && i[1] == 0xfa) {
  10900. NK_ASSERT(nk__dout == output + olen);
  10901. if (nk__dout != output + olen) return 0;
  10902. if (nk_adler32(1, output, olen) != (unsigned int) nk__in4(2))
  10903. return 0;
  10904. return olen;
  10905. } else {
  10906. NK_ASSERT(0); /* NOTREACHED */
  10907. return 0;
  10908. }
  10909. }
  10910. NK_ASSERT(nk__dout <= output + olen);
  10911. if (nk__dout > output + olen)
  10912. return 0;
  10913. }
  10914. }
  10915. NK_INTERN unsigned int
  10916. nk_decode_85_byte(char c)
  10917. { return (unsigned int)((c >= '\\') ? c-36 : c-35); }
  10918. NK_INTERN void
  10919. nk_decode_85(unsigned char* dst, const unsigned char* src)
  10920. {
  10921. while (*src)
  10922. {
  10923. unsigned int tmp =
  10924. nk_decode_85_byte((char)src[0]) +
  10925. 85 * (nk_decode_85_byte((char)src[1]) +
  10926. 85 * (nk_decode_85_byte((char)src[2]) +
  10927. 85 * (nk_decode_85_byte((char)src[3]) +
  10928. 85 * nk_decode_85_byte((char)src[4]))));
  10929. /* we can't assume little-endianess. */
  10930. dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
  10931. dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
  10932. dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
  10933. dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
  10934. src += 5;
  10935. dst += 4;
  10936. }
  10937. }
  10938. /* -------------------------------------------------------------
  10939. *
  10940. * FONT ATLAS
  10941. *
  10942. * --------------------------------------------------------------*/
  10943. NK_API struct nk_font_config
  10944. nk_font_config(float pixel_height)
  10945. {
  10946. struct nk_font_config cfg;
  10947. nk_zero_struct(cfg);
  10948. cfg.ttf_blob = 0;
  10949. cfg.ttf_size = 0;
  10950. cfg.ttf_data_owned_by_atlas = 0;
  10951. cfg.size = pixel_height;
  10952. cfg.oversample_h = 3;
  10953. cfg.oversample_v = 1;
  10954. cfg.pixel_snap = 0;
  10955. cfg.coord_type = NK_COORD_UV;
  10956. cfg.spacing = nk_vec2(0,0);
  10957. cfg.range = nk_font_default_glyph_ranges();
  10958. cfg.merge_mode = 0;
  10959. cfg.fallback_glyph = '?';
  10960. cfg.font = 0;
  10961. return cfg;
  10962. }
  10963. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  10964. NK_API void
  10965. nk_font_atlas_init_default(struct nk_font_atlas *atlas)
  10966. {
  10967. NK_ASSERT(atlas);
  10968. if (!atlas) return;
  10969. nk_zero_struct(*atlas);
  10970. atlas->temporary.userdata.ptr = 0;
  10971. atlas->temporary.alloc = nk_malloc;
  10972. atlas->temporary.free = nk_mfree;
  10973. atlas->permanent.userdata.ptr = 0;
  10974. atlas->permanent.alloc = nk_malloc;
  10975. atlas->permanent.free = nk_mfree;
  10976. }
  10977. #endif
  10978. NK_API void
  10979. nk_font_atlas_init(struct nk_font_atlas *atlas, struct nk_allocator *alloc)
  10980. {
  10981. NK_ASSERT(atlas);
  10982. NK_ASSERT(alloc);
  10983. if (!atlas || !alloc) return;
  10984. nk_zero_struct(*atlas);
  10985. atlas->permanent = *alloc;
  10986. atlas->temporary = *alloc;
  10987. }
  10988. NK_API void
  10989. nk_font_atlas_init_custom(struct nk_font_atlas *atlas,
  10990. struct nk_allocator *permanent, struct nk_allocator *temporary)
  10991. {
  10992. NK_ASSERT(atlas);
  10993. NK_ASSERT(permanent);
  10994. NK_ASSERT(temporary);
  10995. if (!atlas || !permanent || !temporary) return;
  10996. nk_zero_struct(*atlas);
  10997. atlas->permanent = *permanent;
  10998. atlas->temporary = *temporary;
  10999. }
  11000. NK_API void
  11001. nk_font_atlas_begin(struct nk_font_atlas *atlas)
  11002. {
  11003. NK_ASSERT(atlas);
  11004. NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
  11005. NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
  11006. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
  11007. !atlas->temporary.alloc || !atlas->temporary.free) return;
  11008. if (atlas->glyphs) {
  11009. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  11010. atlas->glyphs = 0;
  11011. }
  11012. if (atlas->pixel) {
  11013. atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
  11014. atlas->pixel = 0;
  11015. }
  11016. }
  11017. NK_API struct nk_font*
  11018. nk_font_atlas_add(struct nk_font_atlas *atlas, const struct nk_font_config *config)
  11019. {
  11020. struct nk_font *font = 0;
  11021. struct nk_font_config *cfg;
  11022. NK_ASSERT(atlas);
  11023. NK_ASSERT(atlas->permanent.alloc);
  11024. NK_ASSERT(atlas->permanent.free);
  11025. NK_ASSERT(atlas->temporary.alloc);
  11026. NK_ASSERT(atlas->temporary.free);
  11027. NK_ASSERT(config);
  11028. NK_ASSERT(config->ttf_blob);
  11029. NK_ASSERT(config->ttf_size);
  11030. NK_ASSERT(config->size > 0.0f);
  11031. if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f||
  11032. !atlas->permanent.alloc || !atlas->permanent.free ||
  11033. !atlas->temporary.alloc || !atlas->temporary.free)
  11034. return 0;
  11035. /* allocate and insert font config into list */
  11036. cfg = (struct nk_font_config*)
  11037. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font_config));
  11038. NK_MEMCPY(cfg, config, sizeof(*config));
  11039. if (!atlas->config) {
  11040. atlas->config = cfg;
  11041. cfg->next = 0;
  11042. } else {
  11043. cfg->next = atlas->config;
  11044. atlas->config = cfg;
  11045. }
  11046. /* allocate new font */
  11047. if (!config->merge_mode) {
  11048. font = (struct nk_font*)
  11049. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font));
  11050. NK_ASSERT(font);
  11051. if (!font) return 0;
  11052. font->config = cfg;
  11053. } else {
  11054. NK_ASSERT(atlas->font_num);
  11055. font = atlas->fonts;
  11056. font->config = cfg;
  11057. }
  11058. /* insert font into list */
  11059. if (!config->merge_mode) {
  11060. if (!atlas->fonts) {
  11061. atlas->fonts = font;
  11062. font->next = 0;
  11063. } else {
  11064. font->next = atlas->fonts;
  11065. atlas->fonts = font;
  11066. }
  11067. cfg->font = &font->info;
  11068. }
  11069. /* create own copy of .TTF font blob */
  11070. if (!config->ttf_data_owned_by_atlas) {
  11071. cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata,0, cfg->ttf_size);
  11072. NK_ASSERT(cfg->ttf_blob);
  11073. if (!cfg->ttf_blob) {
  11074. atlas->font_num++;
  11075. return 0;
  11076. }
  11077. NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
  11078. cfg->ttf_data_owned_by_atlas = 1;
  11079. }
  11080. atlas->font_num++;
  11081. return font;
  11082. }
  11083. NK_API struct nk_font*
  11084. nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory,
  11085. nk_size size, float height, const struct nk_font_config *config)
  11086. {
  11087. struct nk_font_config cfg;
  11088. NK_ASSERT(memory);
  11089. NK_ASSERT(size);
  11090. NK_ASSERT(atlas);
  11091. NK_ASSERT(atlas->temporary.alloc);
  11092. NK_ASSERT(atlas->temporary.free);
  11093. NK_ASSERT(atlas->permanent.alloc);
  11094. NK_ASSERT(atlas->permanent.free);
  11095. if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
  11096. !atlas->permanent.alloc || !atlas->permanent.free)
  11097. return 0;
  11098. cfg = (config) ? *config: nk_font_config(height);
  11099. cfg.ttf_blob = memory;
  11100. cfg.ttf_size = size;
  11101. cfg.size = height;
  11102. cfg.ttf_data_owned_by_atlas = 0;
  11103. return nk_font_atlas_add(atlas, &cfg);
  11104. }
  11105. #ifdef NK_INCLUDE_STANDARD_IO
  11106. NK_API struct nk_font*
  11107. nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path,
  11108. float height, const struct nk_font_config *config)
  11109. {
  11110. nk_size size;
  11111. char *memory;
  11112. struct nk_font_config cfg;
  11113. NK_ASSERT(atlas);
  11114. NK_ASSERT(atlas->temporary.alloc);
  11115. NK_ASSERT(atlas->temporary.free);
  11116. NK_ASSERT(atlas->permanent.alloc);
  11117. NK_ASSERT(atlas->permanent.free);
  11118. if (!atlas || !file_path) return 0;
  11119. memory = nk_file_load(file_path, &size, &atlas->permanent);
  11120. if (!memory) return 0;
  11121. cfg = (config) ? *config: nk_font_config(height);
  11122. cfg.ttf_blob = memory;
  11123. cfg.ttf_size = size;
  11124. cfg.size = height;
  11125. cfg.ttf_data_owned_by_atlas = 1;
  11126. return nk_font_atlas_add(atlas, &cfg);
  11127. }
  11128. #endif
  11129. NK_API struct nk_font*
  11130. nk_font_atlas_add_compressed(struct nk_font_atlas *atlas,
  11131. void *compressed_data, nk_size compressed_size, float height,
  11132. const struct nk_font_config *config)
  11133. {
  11134. unsigned int decompressed_size;
  11135. void *decompressed_data;
  11136. struct nk_font_config cfg;
  11137. NK_ASSERT(atlas);
  11138. NK_ASSERT(atlas->temporary.alloc);
  11139. NK_ASSERT(atlas->temporary.free);
  11140. NK_ASSERT(atlas->permanent.alloc);
  11141. NK_ASSERT(atlas->permanent.free);
  11142. NK_ASSERT(compressed_data);
  11143. NK_ASSERT(compressed_size);
  11144. if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
  11145. !atlas->permanent.alloc || !atlas->permanent.free)
  11146. return 0;
  11147. decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
  11148. decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata,0,decompressed_size);
  11149. NK_ASSERT(decompressed_data);
  11150. if (!decompressed_data) return 0;
  11151. nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
  11152. (unsigned int)compressed_size);
  11153. cfg = (config) ? *config: nk_font_config(height);
  11154. cfg.ttf_blob = decompressed_data;
  11155. cfg.ttf_size = decompressed_size;
  11156. cfg.size = height;
  11157. cfg.ttf_data_owned_by_atlas = 1;
  11158. return nk_font_atlas_add(atlas, &cfg);
  11159. }
  11160. NK_API struct nk_font*
  11161. nk_font_atlas_add_compressed_base85(struct nk_font_atlas *atlas,
  11162. const char *data_base85, float height, const struct nk_font_config *config)
  11163. {
  11164. int compressed_size;
  11165. void *compressed_data;
  11166. struct nk_font *font;
  11167. NK_ASSERT(atlas);
  11168. NK_ASSERT(atlas->temporary.alloc);
  11169. NK_ASSERT(atlas->temporary.free);
  11170. NK_ASSERT(atlas->permanent.alloc);
  11171. NK_ASSERT(atlas->permanent.free);
  11172. NK_ASSERT(data_base85);
  11173. if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
  11174. !atlas->permanent.alloc || !atlas->permanent.free)
  11175. return 0;
  11176. compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
  11177. compressed_data = atlas->temporary.alloc(atlas->temporary.userdata,0, (nk_size)compressed_size);
  11178. NK_ASSERT(compressed_data);
  11179. if (!compressed_data) return 0;
  11180. nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
  11181. font = nk_font_atlas_add_compressed(atlas, compressed_data,
  11182. (nk_size)compressed_size, height, config);
  11183. atlas->temporary.free(atlas->temporary.userdata, compressed_data);
  11184. return font;
  11185. }
  11186. #ifdef NK_INCLUDE_DEFAULT_FONT
  11187. NK_API struct nk_font*
  11188. nk_font_atlas_add_default(struct nk_font_atlas *atlas,
  11189. float pixel_height, const struct nk_font_config *config)
  11190. {
  11191. NK_ASSERT(atlas);
  11192. NK_ASSERT(atlas->temporary.alloc);
  11193. NK_ASSERT(atlas->temporary.free);
  11194. NK_ASSERT(atlas->permanent.alloc);
  11195. NK_ASSERT(atlas->permanent.free);
  11196. return nk_font_atlas_add_compressed_base85(atlas,
  11197. nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
  11198. }
  11199. #endif
  11200. NK_API const void*
  11201. nk_font_atlas_bake(struct nk_font_atlas *atlas, int *width, int *height,
  11202. enum nk_font_atlas_format fmt)
  11203. {
  11204. int i = 0;
  11205. void *tmp = 0;
  11206. nk_size tmp_size, img_size;
  11207. struct nk_font *font_iter;
  11208. struct nk_font_baker *baker;
  11209. NK_ASSERT(atlas);
  11210. NK_ASSERT(atlas->temporary.alloc);
  11211. NK_ASSERT(atlas->temporary.free);
  11212. NK_ASSERT(atlas->permanent.alloc);
  11213. NK_ASSERT(atlas->permanent.free);
  11214. NK_ASSERT(width);
  11215. NK_ASSERT(height);
  11216. if (!atlas || !width || !height ||
  11217. !atlas->temporary.alloc || !atlas->temporary.free ||
  11218. !atlas->permanent.alloc || !atlas->permanent.free)
  11219. return 0;
  11220. #ifdef NK_INCLUDE_DEFAULT_FONT
  11221. /* no font added so just use default font */
  11222. if (!atlas->font_num)
  11223. atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
  11224. #endif
  11225. NK_ASSERT(atlas->font_num);
  11226. if (!atlas->font_num) return 0;
  11227. /* allocate temporary baker memory required for the baking process */
  11228. nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
  11229. tmp = atlas->temporary.alloc(atlas->temporary.userdata,0, tmp_size);
  11230. NK_ASSERT(tmp);
  11231. if (!tmp) goto failed;
  11232. /* allocate glyph memory for all fonts */
  11233. baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
  11234. atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
  11235. atlas->permanent.userdata,0, sizeof(struct nk_font_glyph)*(nk_size)atlas->glyph_count);
  11236. NK_ASSERT(atlas->glyphs);
  11237. if (!atlas->glyphs)
  11238. goto failed;
  11239. /* pack all glyphs into a tight fit space */
  11240. atlas->custom.w = (NK_CURSOR_DATA_W*2)+1;
  11241. atlas->custom.h = NK_CURSOR_DATA_H + 1;
  11242. if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
  11243. atlas->config, atlas->font_num, &atlas->temporary))
  11244. goto failed;
  11245. /* allocate memory for the baked image font atlas */
  11246. atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata,0, img_size);
  11247. NK_ASSERT(atlas->pixel);
  11248. if (!atlas->pixel)
  11249. goto failed;
  11250. /* bake glyphs and custom white pixel into image */
  11251. nk_font_bake(baker, atlas->pixel, *width, *height,
  11252. atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
  11253. nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
  11254. nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
  11255. if (fmt == NK_FONT_ATLAS_RGBA32) {
  11256. /* convert alpha8 image into rgba32 image */
  11257. void *img_rgba = atlas->temporary.alloc(atlas->temporary.userdata,0,
  11258. (nk_size)(*width * *height * 4));
  11259. NK_ASSERT(img_rgba);
  11260. if (!img_rgba) goto failed;
  11261. nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
  11262. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  11263. atlas->pixel = img_rgba;
  11264. }
  11265. atlas->tex_width = *width;
  11266. atlas->tex_height = *height;
  11267. /* initialize each font */
  11268. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  11269. struct nk_font *font = font_iter;
  11270. struct nk_font_config *config = font->config;
  11271. nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
  11272. config->font, nk_handle_ptr(0));
  11273. }
  11274. /* initialize each cursor */
  11275. {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
  11276. /* Pos ----- Size ------- Offset --*/
  11277. {{ 0, 3}, {12,19}, { 0, 0}},
  11278. {{13, 0}, { 7,16}, { 4, 8}},
  11279. {{31, 0}, {23,23}, {11,11}},
  11280. {{21, 0}, { 9, 23}, { 5,11}},
  11281. {{55,18}, {23, 9}, {11, 5}},
  11282. {{73, 0}, {17,17}, { 9, 9}},
  11283. {{55, 0}, {17,17}, { 9, 9}}
  11284. };
  11285. for (i = 0; i < NK_CURSOR_COUNT; ++i) {
  11286. struct nk_cursor *cursor = &atlas->cursors[i];
  11287. cursor->img.w = (unsigned short)*width;
  11288. cursor->img.h = (unsigned short)*height;
  11289. cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
  11290. cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
  11291. cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
  11292. cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
  11293. cursor->size = nk_cursor_data[i][1];
  11294. cursor->offset = nk_cursor_data[i][2];
  11295. }}
  11296. /* free temporary memory */
  11297. atlas->temporary.free(atlas->temporary.userdata, tmp);
  11298. return atlas->pixel;
  11299. failed:
  11300. /* error so cleanup all memory */
  11301. if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
  11302. if (atlas->glyphs) {
  11303. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  11304. atlas->glyphs = 0;
  11305. }
  11306. if (atlas->pixel) {
  11307. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  11308. atlas->pixel = 0;
  11309. }
  11310. return 0;
  11311. }
  11312. NK_API void
  11313. nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
  11314. struct nk_draw_null_texture *null)
  11315. {
  11316. int i = 0;
  11317. struct nk_font *font_iter;
  11318. NK_ASSERT(atlas);
  11319. if (!atlas) {
  11320. if (!null) return;
  11321. null->texture = texture;
  11322. null->uv = nk_vec2(0.5f,0.5f);
  11323. }
  11324. if (null) {
  11325. null->texture = texture;
  11326. null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
  11327. null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
  11328. }
  11329. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  11330. font_iter->texture = texture;
  11331. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  11332. font_iter->handle.texture = texture;
  11333. #endif
  11334. }
  11335. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  11336. atlas->cursors[i].img.handle = texture;
  11337. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  11338. atlas->pixel = 0;
  11339. atlas->tex_width = 0;
  11340. atlas->tex_height = 0;
  11341. atlas->custom.x = 0;
  11342. atlas->custom.y = 0;
  11343. atlas->custom.w = 0;
  11344. atlas->custom.h = 0;
  11345. }
  11346. NK_API void
  11347. nk_font_atlas_cleanup(struct nk_font_atlas *atlas)
  11348. {
  11349. NK_ASSERT(atlas);
  11350. NK_ASSERT(atlas->temporary.alloc);
  11351. NK_ASSERT(atlas->temporary.free);
  11352. NK_ASSERT(atlas->permanent.alloc);
  11353. NK_ASSERT(atlas->permanent.free);
  11354. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  11355. if (atlas->config) {
  11356. struct nk_font_config *iter, *next;
  11357. for (iter = atlas->config; iter; iter = next) {
  11358. next = iter->next;
  11359. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  11360. atlas->permanent.free(atlas->permanent.userdata, iter);
  11361. }
  11362. atlas->config = 0;
  11363. }
  11364. }
  11365. NK_API void
  11366. nk_font_atlas_clear(struct nk_font_atlas *atlas)
  11367. {
  11368. NK_ASSERT(atlas);
  11369. NK_ASSERT(atlas->temporary.alloc);
  11370. NK_ASSERT(atlas->temporary.free);
  11371. NK_ASSERT(atlas->permanent.alloc);
  11372. NK_ASSERT(atlas->permanent.free);
  11373. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  11374. nk_font_atlas_cleanup(atlas);
  11375. if (atlas->fonts) {
  11376. struct nk_font *iter, *next;
  11377. for (iter = atlas->fonts; iter; iter = next) {
  11378. next = iter->next;
  11379. atlas->permanent.free(atlas->permanent.userdata, iter);
  11380. }
  11381. atlas->fonts = 0;
  11382. }
  11383. if (atlas->glyphs)
  11384. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  11385. nk_zero_struct(*atlas);
  11386. }
  11387. #endif
  11388. /* ==============================================================
  11389. *
  11390. * INPUT
  11391. *
  11392. * ===============================================================*/
  11393. NK_API void
  11394. nk_input_begin(struct nk_context *ctx)
  11395. {
  11396. int i;
  11397. struct nk_input *in;
  11398. NK_ASSERT(ctx);
  11399. if (!ctx) return;
  11400. in = &ctx->input;
  11401. for (i = 0; i < NK_BUTTON_MAX; ++i)
  11402. in->mouse.buttons[i].clicked = 0;
  11403. in->keyboard.text_len = 0;
  11404. in->mouse.scroll_delta = nk_vec2(0,0);
  11405. in->mouse.prev.x = in->mouse.pos.x;
  11406. in->mouse.prev.y = in->mouse.pos.y;
  11407. in->mouse.delta.x = 0;
  11408. in->mouse.delta.y = 0;
  11409. for (i = 0; i < NK_KEY_MAX; i++)
  11410. in->keyboard.keys[i].clicked = 0;
  11411. }
  11412. NK_API void
  11413. nk_input_end(struct nk_context *ctx)
  11414. {
  11415. struct nk_input *in;
  11416. NK_ASSERT(ctx);
  11417. if (!ctx) return;
  11418. in = &ctx->input;
  11419. if (in->mouse.grab)
  11420. in->mouse.grab = 0;
  11421. if (in->mouse.ungrab) {
  11422. in->mouse.grabbed = 0;
  11423. in->mouse.ungrab = 0;
  11424. in->mouse.grab = 0;
  11425. }
  11426. }
  11427. NK_API void
  11428. nk_input_motion(struct nk_context *ctx, int x, int y)
  11429. {
  11430. struct nk_input *in;
  11431. NK_ASSERT(ctx);
  11432. if (!ctx) return;
  11433. in = &ctx->input;
  11434. in->mouse.pos.x = (float)x;
  11435. in->mouse.pos.y = (float)y;
  11436. in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
  11437. in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
  11438. }
  11439. NK_API void
  11440. nk_input_key(struct nk_context *ctx, enum nk_keys key, int down)
  11441. {
  11442. struct nk_input *in;
  11443. NK_ASSERT(ctx);
  11444. if (!ctx) return;
  11445. in = &ctx->input;
  11446. if (in->keyboard.keys[key].down != down)
  11447. in->keyboard.keys[key].clicked++;
  11448. in->keyboard.keys[key].down = down;
  11449. }
  11450. NK_API void
  11451. nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, int down)
  11452. {
  11453. struct nk_mouse_button *btn;
  11454. struct nk_input *in;
  11455. NK_ASSERT(ctx);
  11456. if (!ctx) return;
  11457. in = &ctx->input;
  11458. if (in->mouse.buttons[id].down == down) return;
  11459. btn = &in->mouse.buttons[id];
  11460. btn->clicked_pos.x = (float)x;
  11461. btn->clicked_pos.y = (float)y;
  11462. btn->down = down;
  11463. btn->clicked++;
  11464. }
  11465. NK_API void
  11466. nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
  11467. {
  11468. NK_ASSERT(ctx);
  11469. if (!ctx) return;
  11470. ctx->input.mouse.scroll_delta.x += val.x;
  11471. ctx->input.mouse.scroll_delta.y += val.y;
  11472. }
  11473. NK_API void
  11474. nk_input_glyph(struct nk_context *ctx, const nk_glyph glyph)
  11475. {
  11476. int len = 0;
  11477. nk_rune unicode;
  11478. struct nk_input *in;
  11479. NK_ASSERT(ctx);
  11480. if (!ctx) return;
  11481. in = &ctx->input;
  11482. len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
  11483. if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
  11484. nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
  11485. NK_INPUT_MAX - in->keyboard.text_len);
  11486. in->keyboard.text_len += len;
  11487. }
  11488. }
  11489. NK_API void
  11490. nk_input_char(struct nk_context *ctx, char c)
  11491. {
  11492. nk_glyph glyph;
  11493. NK_ASSERT(ctx);
  11494. if (!ctx) return;
  11495. glyph[0] = c;
  11496. nk_input_glyph(ctx, glyph);
  11497. }
  11498. NK_API void
  11499. nk_input_unicode(struct nk_context *ctx, nk_rune unicode)
  11500. {
  11501. nk_glyph rune;
  11502. NK_ASSERT(ctx);
  11503. if (!ctx) return;
  11504. nk_utf_encode(unicode, rune, NK_UTF_SIZE);
  11505. nk_input_glyph(ctx, rune);
  11506. }
  11507. NK_API int
  11508. nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
  11509. {
  11510. const struct nk_mouse_button *btn;
  11511. if (!i) return nk_false;
  11512. btn = &i->mouse.buttons[id];
  11513. return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
  11514. }
  11515. NK_API int
  11516. nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  11517. struct nk_rect b)
  11518. {
  11519. const struct nk_mouse_button *btn;
  11520. if (!i) return nk_false;
  11521. btn = &i->mouse.buttons[id];
  11522. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
  11523. return nk_false;
  11524. return nk_true;
  11525. }
  11526. NK_API int
  11527. nk_input_has_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  11528. struct nk_rect b, int down)
  11529. {
  11530. const struct nk_mouse_button *btn;
  11531. if (!i) return nk_false;
  11532. btn = &i->mouse.buttons[id];
  11533. return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
  11534. }
  11535. NK_API int
  11536. nk_input_is_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  11537. struct nk_rect b)
  11538. {
  11539. const struct nk_mouse_button *btn;
  11540. if (!i) return nk_false;
  11541. btn = &i->mouse.buttons[id];
  11542. return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
  11543. btn->clicked) ? nk_true : nk_false;
  11544. }
  11545. NK_API int
  11546. nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  11547. struct nk_rect b, int down)
  11548. {
  11549. const struct nk_mouse_button *btn;
  11550. if (!i) return nk_false;
  11551. btn = &i->mouse.buttons[id];
  11552. return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
  11553. btn->clicked) ? nk_true : nk_false;
  11554. }
  11555. NK_API int
  11556. nk_input_any_mouse_click_in_rect(const struct nk_input *in, struct nk_rect b)
  11557. {
  11558. int i, down = 0;
  11559. for (i = 0; i < NK_BUTTON_MAX; ++i)
  11560. down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
  11561. return down;
  11562. }
  11563. NK_API int
  11564. nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  11565. {
  11566. if (!i) return nk_false;
  11567. return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
  11568. }
  11569. NK_API int
  11570. nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  11571. {
  11572. if (!i) return nk_false;
  11573. return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
  11574. }
  11575. NK_API int
  11576. nk_input_mouse_clicked(const struct nk_input *i, enum nk_buttons id, struct nk_rect rect)
  11577. {
  11578. if (!i) return nk_false;
  11579. if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
  11580. return nk_input_is_mouse_click_in_rect(i, id, rect);
  11581. }
  11582. NK_API int
  11583. nk_input_is_mouse_down(const struct nk_input *i, enum nk_buttons id)
  11584. {
  11585. if (!i) return nk_false;
  11586. return i->mouse.buttons[id].down;
  11587. }
  11588. NK_API int
  11589. nk_input_is_mouse_pressed(const struct nk_input *i, enum nk_buttons id)
  11590. {
  11591. const struct nk_mouse_button *b;
  11592. if (!i) return nk_false;
  11593. b = &i->mouse.buttons[id];
  11594. if (b->down && b->clicked)
  11595. return nk_true;
  11596. return nk_false;
  11597. }
  11598. NK_API int
  11599. nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
  11600. {
  11601. if (!i) return nk_false;
  11602. return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
  11603. }
  11604. NK_API int
  11605. nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
  11606. {
  11607. const struct nk_key *k;
  11608. if (!i) return nk_false;
  11609. k = &i->keyboard.keys[key];
  11610. if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
  11611. return nk_true;
  11612. return nk_false;
  11613. }
  11614. NK_API int
  11615. nk_input_is_key_released(const struct nk_input *i, enum nk_keys key)
  11616. {
  11617. const struct nk_key *k;
  11618. if (!i) return nk_false;
  11619. k = &i->keyboard.keys[key];
  11620. if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
  11621. return nk_true;
  11622. return nk_false;
  11623. }
  11624. NK_API int
  11625. nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
  11626. {
  11627. const struct nk_key *k;
  11628. if (!i) return nk_false;
  11629. k = &i->keyboard.keys[key];
  11630. if (k->down) return nk_true;
  11631. return nk_false;
  11632. }
  11633. /*
  11634. * ==============================================================
  11635. *
  11636. * TEXT EDITOR
  11637. *
  11638. * ===============================================================
  11639. */
  11640. /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
  11641. struct nk_text_find {
  11642. float x,y; /* position of n'th character */
  11643. float height; /* height of line */
  11644. int first_char, length; /* first char of row, and length */
  11645. int prev_first; /*_ first char of previous row */
  11646. };
  11647. struct nk_text_edit_row {
  11648. float x0,x1;
  11649. /* starting x location, end x location (allows for align=right, etc) */
  11650. float baseline_y_delta;
  11651. /* position of baseline relative to previous row's baseline*/
  11652. float ymin,ymax;
  11653. /* height of row above and below baseline */
  11654. int num_chars;
  11655. };
  11656. /* forward declarations */
  11657. NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
  11658. NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
  11659. NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
  11660. #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
  11661. NK_INTERN float
  11662. nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
  11663. const struct nk_user_font *font)
  11664. {
  11665. int len = 0;
  11666. nk_rune unicode = 0;
  11667. const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
  11668. return font->width(font->userdata, font->height, str, len);
  11669. }
  11670. NK_INTERN void
  11671. nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
  11672. int line_start_id, float row_height, const struct nk_user_font *font)
  11673. {
  11674. int l;
  11675. int glyphs = 0;
  11676. nk_rune unicode;
  11677. const char *remaining;
  11678. int len = nk_str_len_char(&edit->string);
  11679. const char *end = nk_str_get_const(&edit->string) + len;
  11680. const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
  11681. const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
  11682. text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
  11683. r->x0 = 0.0f;
  11684. r->x1 = size.x;
  11685. r->baseline_y_delta = size.y;
  11686. r->ymin = 0.0f;
  11687. r->ymax = size.y;
  11688. r->num_chars = glyphs;
  11689. }
  11690. NK_INTERN int
  11691. nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
  11692. const struct nk_user_font *font, float row_height)
  11693. {
  11694. struct nk_text_edit_row r;
  11695. int n = edit->string.len;
  11696. float base_y = 0, prev_x;
  11697. int i=0, k;
  11698. r.x0 = r.x1 = 0;
  11699. r.ymin = r.ymax = 0;
  11700. r.num_chars = 0;
  11701. /* search rows to find one that straddles 'y' */
  11702. while (i < n) {
  11703. nk_textedit_layout_row(&r, edit, i, row_height, font);
  11704. if (r.num_chars <= 0)
  11705. return n;
  11706. if (i==0 && y < base_y + r.ymin)
  11707. return 0;
  11708. if (y < base_y + r.ymax)
  11709. break;
  11710. i += r.num_chars;
  11711. base_y += r.baseline_y_delta;
  11712. }
  11713. /* below all text, return 'after' last character */
  11714. if (i >= n)
  11715. return n;
  11716. /* check if it's before the beginning of the line */
  11717. if (x < r.x0)
  11718. return i;
  11719. /* check if it's before the end of the line */
  11720. if (x < r.x1) {
  11721. /* search characters in row for one that straddles 'x' */
  11722. k = i;
  11723. prev_x = r.x0;
  11724. for (i=0; i < r.num_chars; ++i) {
  11725. float w = nk_textedit_get_width(edit, k, i, font);
  11726. if (x < prev_x+w) {
  11727. if (x < prev_x+w/2)
  11728. return k+i;
  11729. else return k+i+1;
  11730. }
  11731. prev_x += w;
  11732. }
  11733. /* shouldn't happen, but if it does, fall through to end-of-line case */
  11734. }
  11735. /* if the last character is a newline, return that.
  11736. * otherwise return 'after' the last character */
  11737. if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
  11738. return i+r.num_chars-1;
  11739. else return i+r.num_chars;
  11740. }
  11741. NK_INTERN void
  11742. nk_textedit_click(struct nk_text_edit *state, float x, float y,
  11743. const struct nk_user_font *font, float row_height)
  11744. {
  11745. /* API click: on mouse down, move the cursor to the clicked location,
  11746. * and reset the selection */
  11747. state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
  11748. state->select_start = state->cursor;
  11749. state->select_end = state->cursor;
  11750. state->has_preferred_x = 0;
  11751. }
  11752. NK_INTERN void
  11753. nk_textedit_drag(struct nk_text_edit *state, float x, float y,
  11754. const struct nk_user_font *font, float row_height)
  11755. {
  11756. /* API drag: on mouse drag, move the cursor and selection endpoint
  11757. * to the clicked location */
  11758. int p = nk_textedit_locate_coord(state, x, y, font, row_height);
  11759. if (state->select_start == state->select_end)
  11760. state->select_start = state->cursor;
  11761. state->cursor = state->select_end = p;
  11762. }
  11763. NK_INTERN void
  11764. nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
  11765. int n, int single_line, const struct nk_user_font *font, float row_height)
  11766. {
  11767. /* find the x/y location of a character, and remember info about the previous
  11768. * row in case we get a move-up event (for page up, we'll have to rescan) */
  11769. struct nk_text_edit_row r;
  11770. int prev_start = 0;
  11771. int z = state->string.len;
  11772. int i=0, first;
  11773. nk_zero_struct(r);
  11774. if (n == z) {
  11775. /* if it's at the end, then find the last line -- simpler than trying to
  11776. explicitly handle this case in the regular code */
  11777. nk_textedit_layout_row(&r, state, 0, row_height, font);
  11778. if (single_line) {
  11779. find->first_char = 0;
  11780. find->length = z;
  11781. } else {
  11782. while (i < z) {
  11783. prev_start = i;
  11784. i += r.num_chars;
  11785. nk_textedit_layout_row(&r, state, i, row_height, font);
  11786. }
  11787. find->first_char = i;
  11788. find->length = r.num_chars;
  11789. }
  11790. find->x = r.x1;
  11791. find->y = r.ymin;
  11792. find->height = r.ymax - r.ymin;
  11793. find->prev_first = prev_start;
  11794. return;
  11795. }
  11796. /* search rows to find the one that straddles character n */
  11797. find->y = 0;
  11798. for(;;) {
  11799. nk_textedit_layout_row(&r, state, i, row_height, font);
  11800. if (n < i + r.num_chars) break;
  11801. prev_start = i;
  11802. i += r.num_chars;
  11803. find->y += r.baseline_y_delta;
  11804. }
  11805. find->first_char = first = i;
  11806. find->length = r.num_chars;
  11807. find->height = r.ymax - r.ymin;
  11808. find->prev_first = prev_start;
  11809. /* now scan to find xpos */
  11810. find->x = r.x0;
  11811. for (i=0; first+i < n; ++i)
  11812. find->x += nk_textedit_get_width(state, first, i, font);
  11813. }
  11814. NK_INTERN void
  11815. nk_textedit_clamp(struct nk_text_edit *state)
  11816. {
  11817. /* make the selection/cursor state valid if client altered the string */
  11818. int n = state->string.len;
  11819. if (NK_TEXT_HAS_SELECTION(state)) {
  11820. if (state->select_start > n) state->select_start = n;
  11821. if (state->select_end > n) state->select_end = n;
  11822. /* if clamping forced them to be equal, move the cursor to match */
  11823. if (state->select_start == state->select_end)
  11824. state->cursor = state->select_start;
  11825. }
  11826. if (state->cursor > n) state->cursor = n;
  11827. }
  11828. NK_API void
  11829. nk_textedit_delete(struct nk_text_edit *state, int where, int len)
  11830. {
  11831. /* delete characters while updating undo */
  11832. nk_textedit_makeundo_delete(state, where, len);
  11833. nk_str_delete_runes(&state->string, where, len);
  11834. state->has_preferred_x = 0;
  11835. }
  11836. NK_API void
  11837. nk_textedit_delete_selection(struct nk_text_edit *state)
  11838. {
  11839. /* delete the section */
  11840. nk_textedit_clamp(state);
  11841. if (NK_TEXT_HAS_SELECTION(state)) {
  11842. if (state->select_start < state->select_end) {
  11843. nk_textedit_delete(state, state->select_start,
  11844. state->select_end - state->select_start);
  11845. state->select_end = state->cursor = state->select_start;
  11846. } else {
  11847. nk_textedit_delete(state, state->select_end,
  11848. state->select_start - state->select_end);
  11849. state->select_start = state->cursor = state->select_end;
  11850. }
  11851. state->has_preferred_x = 0;
  11852. }
  11853. }
  11854. NK_INTERN void
  11855. nk_textedit_sortselection(struct nk_text_edit *state)
  11856. {
  11857. /* canonicalize the selection so start <= end */
  11858. if (state->select_end < state->select_start) {
  11859. int temp = state->select_end;
  11860. state->select_end = state->select_start;
  11861. state->select_start = temp;
  11862. }
  11863. }
  11864. NK_INTERN void
  11865. nk_textedit_move_to_first(struct nk_text_edit *state)
  11866. {
  11867. /* move cursor to first character of selection */
  11868. if (NK_TEXT_HAS_SELECTION(state)) {
  11869. nk_textedit_sortselection(state);
  11870. state->cursor = state->select_start;
  11871. state->select_end = state->select_start;
  11872. state->has_preferred_x = 0;
  11873. }
  11874. }
  11875. NK_INTERN void
  11876. nk_textedit_move_to_last(struct nk_text_edit *state)
  11877. {
  11878. /* move cursor to last character of selection */
  11879. if (NK_TEXT_HAS_SELECTION(state)) {
  11880. nk_textedit_sortselection(state);
  11881. nk_textedit_clamp(state);
  11882. state->cursor = state->select_end;
  11883. state->select_start = state->select_end;
  11884. state->has_preferred_x = 0;
  11885. }
  11886. }
  11887. NK_INTERN int
  11888. nk_is_word_boundary( struct nk_text_edit *state, int idx)
  11889. {
  11890. int len;
  11891. nk_rune c;
  11892. if (idx <= 0) return 1;
  11893. if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
  11894. return (c == ' ' || c == '\t' ||c == 0x3000 || c == ',' || c == ';' ||
  11895. c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' ||
  11896. c == '|');
  11897. }
  11898. NK_INTERN int
  11899. nk_textedit_move_to_word_previous(struct nk_text_edit *state)
  11900. {
  11901. int c = state->cursor - 1;
  11902. while( c >= 0 && !nk_is_word_boundary(state, c))
  11903. --c;
  11904. if( c < 0 )
  11905. c = 0;
  11906. return c;
  11907. }
  11908. NK_INTERN int
  11909. nk_textedit_move_to_word_next(struct nk_text_edit *state)
  11910. {
  11911. const int len = state->string.len;
  11912. int c = state->cursor+1;
  11913. while( c < len && !nk_is_word_boundary(state, c))
  11914. ++c;
  11915. if( c > len )
  11916. c = len;
  11917. return c;
  11918. }
  11919. NK_INTERN void
  11920. nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
  11921. {
  11922. /* update selection and cursor to match each other */
  11923. if (!NK_TEXT_HAS_SELECTION(state))
  11924. state->select_start = state->select_end = state->cursor;
  11925. else state->cursor = state->select_end;
  11926. }
  11927. NK_API int
  11928. nk_textedit_cut(struct nk_text_edit *state)
  11929. {
  11930. /* API cut: delete selection */
  11931. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  11932. return 0;
  11933. if (NK_TEXT_HAS_SELECTION(state)) {
  11934. nk_textedit_delete_selection(state); /* implicitly clamps */
  11935. state->has_preferred_x = 0;
  11936. return 1;
  11937. }
  11938. return 0;
  11939. }
  11940. NK_API int
  11941. nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
  11942. {
  11943. /* API paste: replace existing selection with passed-in text */
  11944. int glyphs;
  11945. const char *text = (const char *) ctext;
  11946. if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
  11947. /* if there's a selection, the paste should delete it */
  11948. nk_textedit_clamp(state);
  11949. nk_textedit_delete_selection(state);
  11950. /* try to insert the characters */
  11951. glyphs = nk_utf_len(ctext, len);
  11952. if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
  11953. nk_textedit_makeundo_insert(state, state->cursor, glyphs);
  11954. state->cursor += len;
  11955. state->has_preferred_x = 0;
  11956. return 1;
  11957. }
  11958. /* remove the undo since we didn't actually insert the characters */
  11959. if (state->undo.undo_point)
  11960. --state->undo.undo_point;
  11961. return 0;
  11962. }
  11963. NK_API void
  11964. nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
  11965. {
  11966. nk_rune unicode;
  11967. int glyph_len;
  11968. int text_len = 0;
  11969. NK_ASSERT(state);
  11970. NK_ASSERT(text);
  11971. if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
  11972. glyph_len = nk_utf_decode(text, &unicode, total_len);
  11973. while ((text_len < total_len) && glyph_len)
  11974. {
  11975. /* don't insert a backward delete, just process the event */
  11976. if (unicode == 127) goto next;
  11977. /* can't add newline in single-line mode */
  11978. if (unicode == '\n' && state->single_line) goto next;
  11979. /* filter incoming text */
  11980. if (state->filter && !state->filter(state, unicode)) goto next;
  11981. if (!NK_TEXT_HAS_SELECTION(state) &&
  11982. state->cursor < state->string.len)
  11983. {
  11984. if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
  11985. nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
  11986. nk_str_delete_runes(&state->string, state->cursor, 1);
  11987. }
  11988. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  11989. text+text_len, 1))
  11990. {
  11991. ++state->cursor;
  11992. state->has_preferred_x = 0;
  11993. }
  11994. } else {
  11995. nk_textedit_delete_selection(state); /* implicitly clamps */
  11996. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  11997. text+text_len, 1))
  11998. {
  11999. nk_textedit_makeundo_insert(state, state->cursor, 1);
  12000. ++state->cursor;
  12001. state->has_preferred_x = 0;
  12002. }
  12003. }
  12004. next:
  12005. text_len += glyph_len;
  12006. glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
  12007. }
  12008. }
  12009. NK_INTERN void
  12010. nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
  12011. const struct nk_user_font *font, float row_height)
  12012. {
  12013. retry:
  12014. switch (key)
  12015. {
  12016. case NK_KEY_NONE:
  12017. case NK_KEY_CTRL:
  12018. case NK_KEY_ENTER:
  12019. case NK_KEY_SHIFT:
  12020. case NK_KEY_TAB:
  12021. case NK_KEY_COPY:
  12022. case NK_KEY_CUT:
  12023. case NK_KEY_PASTE:
  12024. case NK_KEY_MAX:
  12025. default: break;
  12026. case NK_KEY_TEXT_UNDO:
  12027. nk_textedit_undo(state);
  12028. state->has_preferred_x = 0;
  12029. break;
  12030. case NK_KEY_TEXT_REDO:
  12031. nk_textedit_redo(state);
  12032. state->has_preferred_x = 0;
  12033. break;
  12034. case NK_KEY_TEXT_SELECT_ALL:
  12035. nk_textedit_select_all(state);
  12036. state->has_preferred_x = 0;
  12037. break;
  12038. case NK_KEY_TEXT_INSERT_MODE:
  12039. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  12040. state->mode = NK_TEXT_EDIT_MODE_INSERT;
  12041. break;
  12042. case NK_KEY_TEXT_REPLACE_MODE:
  12043. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  12044. state->mode = NK_TEXT_EDIT_MODE_REPLACE;
  12045. break;
  12046. case NK_KEY_TEXT_RESET_MODE:
  12047. if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
  12048. state->mode == NK_TEXT_EDIT_MODE_REPLACE)
  12049. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  12050. break;
  12051. case NK_KEY_LEFT:
  12052. if (shift_mod) {
  12053. nk_textedit_clamp(state);
  12054. nk_textedit_prep_selection_at_cursor(state);
  12055. /* move selection left */
  12056. if (state->select_end > 0)
  12057. --state->select_end;
  12058. state->cursor = state->select_end;
  12059. state->has_preferred_x = 0;
  12060. } else {
  12061. /* if currently there's a selection,
  12062. * move cursor to start of selection */
  12063. if (NK_TEXT_HAS_SELECTION(state))
  12064. nk_textedit_move_to_first(state);
  12065. else if (state->cursor > 0)
  12066. --state->cursor;
  12067. state->has_preferred_x = 0;
  12068. } break;
  12069. case NK_KEY_RIGHT:
  12070. if (shift_mod) {
  12071. nk_textedit_prep_selection_at_cursor(state);
  12072. /* move selection right */
  12073. ++state->select_end;
  12074. nk_textedit_clamp(state);
  12075. state->cursor = state->select_end;
  12076. state->has_preferred_x = 0;
  12077. } else {
  12078. /* if currently there's a selection,
  12079. * move cursor to end of selection */
  12080. if (NK_TEXT_HAS_SELECTION(state))
  12081. nk_textedit_move_to_last(state);
  12082. else ++state->cursor;
  12083. nk_textedit_clamp(state);
  12084. state->has_preferred_x = 0;
  12085. } break;
  12086. case NK_KEY_TEXT_WORD_LEFT:
  12087. if (shift_mod) {
  12088. if( !NK_TEXT_HAS_SELECTION( state ) )
  12089. nk_textedit_prep_selection_at_cursor(state);
  12090. state->cursor = nk_textedit_move_to_word_previous(state);
  12091. state->select_end = state->cursor;
  12092. nk_textedit_clamp(state );
  12093. } else {
  12094. if (NK_TEXT_HAS_SELECTION(state))
  12095. nk_textedit_move_to_first(state);
  12096. else {
  12097. state->cursor = nk_textedit_move_to_word_previous(state);
  12098. nk_textedit_clamp(state );
  12099. }
  12100. } break;
  12101. case NK_KEY_TEXT_WORD_RIGHT:
  12102. if (shift_mod) {
  12103. if( !NK_TEXT_HAS_SELECTION( state ) )
  12104. nk_textedit_prep_selection_at_cursor(state);
  12105. state->cursor = nk_textedit_move_to_word_next(state);
  12106. state->select_end = state->cursor;
  12107. nk_textedit_clamp(state);
  12108. } else {
  12109. if (NK_TEXT_HAS_SELECTION(state))
  12110. nk_textedit_move_to_last(state);
  12111. else {
  12112. state->cursor = nk_textedit_move_to_word_next(state);
  12113. nk_textedit_clamp(state );
  12114. }
  12115. } break;
  12116. case NK_KEY_DOWN: {
  12117. struct nk_text_find find;
  12118. struct nk_text_edit_row row;
  12119. int i, sel = shift_mod;
  12120. if (state->single_line) {
  12121. /* on windows, up&down in single-line behave like left&right */
  12122. key = NK_KEY_RIGHT;
  12123. goto retry;
  12124. }
  12125. if (sel)
  12126. nk_textedit_prep_selection_at_cursor(state);
  12127. else if (NK_TEXT_HAS_SELECTION(state))
  12128. nk_textedit_move_to_last(state);
  12129. /* compute current position of cursor point */
  12130. nk_textedit_clamp(state);
  12131. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  12132. font, row_height);
  12133. /* now find character position down a row */
  12134. if (find.length)
  12135. {
  12136. float x;
  12137. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  12138. int start = find.first_char + find.length;
  12139. state->cursor = start;
  12140. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  12141. x = row.x0;
  12142. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  12143. float dx = nk_textedit_get_width(state, start, i, font);
  12144. x += dx;
  12145. if (x > goal_x)
  12146. break;
  12147. ++state->cursor;
  12148. }
  12149. nk_textedit_clamp(state);
  12150. state->has_preferred_x = 1;
  12151. state->preferred_x = goal_x;
  12152. if (sel)
  12153. state->select_end = state->cursor;
  12154. }
  12155. } break;
  12156. case NK_KEY_UP: {
  12157. struct nk_text_find find;
  12158. struct nk_text_edit_row row;
  12159. int i, sel = shift_mod;
  12160. if (state->single_line) {
  12161. /* on windows, up&down become left&right */
  12162. key = NK_KEY_LEFT;
  12163. goto retry;
  12164. }
  12165. if (sel)
  12166. nk_textedit_prep_selection_at_cursor(state);
  12167. else if (NK_TEXT_HAS_SELECTION(state))
  12168. nk_textedit_move_to_first(state);
  12169. /* compute current position of cursor point */
  12170. nk_textedit_clamp(state);
  12171. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  12172. font, row_height);
  12173. /* can only go up if there's a previous row */
  12174. if (find.prev_first != find.first_char) {
  12175. /* now find character position up a row */
  12176. float x;
  12177. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  12178. state->cursor = find.prev_first;
  12179. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  12180. x = row.x0;
  12181. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  12182. float dx = nk_textedit_get_width(state, find.prev_first, i, font);
  12183. x += dx;
  12184. if (x > goal_x)
  12185. break;
  12186. ++state->cursor;
  12187. }
  12188. nk_textedit_clamp(state);
  12189. state->has_preferred_x = 1;
  12190. state->preferred_x = goal_x;
  12191. if (sel) state->select_end = state->cursor;
  12192. }
  12193. } break;
  12194. case NK_KEY_DEL:
  12195. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  12196. break;
  12197. if (NK_TEXT_HAS_SELECTION(state))
  12198. nk_textedit_delete_selection(state);
  12199. else {
  12200. int n = state->string.len;
  12201. if (state->cursor < n)
  12202. nk_textedit_delete(state, state->cursor, 1);
  12203. }
  12204. state->has_preferred_x = 0;
  12205. break;
  12206. case NK_KEY_BACKSPACE:
  12207. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  12208. break;
  12209. if (NK_TEXT_HAS_SELECTION(state))
  12210. nk_textedit_delete_selection(state);
  12211. else {
  12212. nk_textedit_clamp(state);
  12213. if (state->cursor > 0) {
  12214. nk_textedit_delete(state, state->cursor-1, 1);
  12215. --state->cursor;
  12216. }
  12217. }
  12218. state->has_preferred_x = 0;
  12219. break;
  12220. case NK_KEY_TEXT_START:
  12221. if (shift_mod) {
  12222. nk_textedit_prep_selection_at_cursor(state);
  12223. state->cursor = state->select_end = 0;
  12224. state->has_preferred_x = 0;
  12225. } else {
  12226. state->cursor = state->select_start = state->select_end = 0;
  12227. state->has_preferred_x = 0;
  12228. }
  12229. break;
  12230. case NK_KEY_TEXT_END:
  12231. if (shift_mod) {
  12232. nk_textedit_prep_selection_at_cursor(state);
  12233. state->cursor = state->select_end = state->string.len;
  12234. state->has_preferred_x = 0;
  12235. } else {
  12236. state->cursor = state->string.len;
  12237. state->select_start = state->select_end = 0;
  12238. state->has_preferred_x = 0;
  12239. }
  12240. break;
  12241. case NK_KEY_TEXT_LINE_START: {
  12242. if (shift_mod) {
  12243. struct nk_text_find find;
  12244. nk_textedit_clamp(state);
  12245. nk_textedit_prep_selection_at_cursor(state);
  12246. if (state->string.len && state->cursor == state->string.len)
  12247. --state->cursor;
  12248. nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
  12249. font, row_height);
  12250. state->cursor = state->select_end = find.first_char;
  12251. state->has_preferred_x = 0;
  12252. } else {
  12253. struct nk_text_find find;
  12254. if (state->string.len && state->cursor == state->string.len)
  12255. --state->cursor;
  12256. nk_textedit_clamp(state);
  12257. nk_textedit_move_to_first(state);
  12258. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  12259. font, row_height);
  12260. state->cursor = find.first_char;
  12261. state->has_preferred_x = 0;
  12262. }
  12263. } break;
  12264. case NK_KEY_TEXT_LINE_END: {
  12265. if (shift_mod) {
  12266. struct nk_text_find find;
  12267. nk_textedit_clamp(state);
  12268. nk_textedit_prep_selection_at_cursor(state);
  12269. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  12270. font, row_height);
  12271. state->has_preferred_x = 0;
  12272. state->cursor = find.first_char + find.length;
  12273. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  12274. --state->cursor;
  12275. state->select_end = state->cursor;
  12276. } else {
  12277. struct nk_text_find find;
  12278. nk_textedit_clamp(state);
  12279. nk_textedit_move_to_first(state);
  12280. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  12281. font, row_height);
  12282. state->has_preferred_x = 0;
  12283. state->cursor = find.first_char + find.length;
  12284. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  12285. --state->cursor;
  12286. }} break;
  12287. }
  12288. }
  12289. NK_INTERN void
  12290. nk_textedit_flush_redo(struct nk_text_undo_state *state)
  12291. {
  12292. state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  12293. state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  12294. }
  12295. NK_INTERN void
  12296. nk_textedit_discard_undo(struct nk_text_undo_state *state)
  12297. {
  12298. /* discard the oldest entry in the undo list */
  12299. if (state->undo_point > 0) {
  12300. /* if the 0th undo state has characters, clean those up */
  12301. if (state->undo_rec[0].char_storage >= 0) {
  12302. int n = state->undo_rec[0].insert_length, i;
  12303. /* delete n characters from all other records */
  12304. state->undo_char_point = (short)(state->undo_char_point - n);
  12305. NK_MEMCPY(state->undo_char, state->undo_char + n,
  12306. (nk_size)state->undo_char_point*sizeof(nk_rune));
  12307. for (i=0; i < state->undo_point; ++i) {
  12308. if (state->undo_rec[i].char_storage >= 0)
  12309. state->undo_rec[i].char_storage = (short)
  12310. (state->undo_rec[i].char_storage - n);
  12311. }
  12312. }
  12313. --state->undo_point;
  12314. NK_MEMCPY(state->undo_rec, state->undo_rec+1,
  12315. (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
  12316. }
  12317. }
  12318. NK_INTERN void
  12319. nk_textedit_discard_redo(struct nk_text_undo_state *state)
  12320. {
  12321. /* discard the oldest entry in the redo list--it's bad if this
  12322. ever happens, but because undo & redo have to store the actual
  12323. characters in different cases, the redo character buffer can
  12324. fill up even though the undo buffer didn't */
  12325. nk_size num;
  12326. int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
  12327. if (state->redo_point <= k) {
  12328. /* if the k'th undo state has characters, clean those up */
  12329. if (state->undo_rec[k].char_storage >= 0) {
  12330. int n = state->undo_rec[k].insert_length, i;
  12331. /* delete n characters from all other records */
  12332. state->redo_char_point = (short)(state->redo_char_point + n);
  12333. num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
  12334. NK_MEMCPY(state->undo_char + state->redo_char_point,
  12335. state->undo_char + state->redo_char_point-n, num * sizeof(char));
  12336. for (i = state->redo_point; i < k; ++i) {
  12337. if (state->undo_rec[i].char_storage >= 0) {
  12338. state->undo_rec[i].char_storage = (short)
  12339. (state->undo_rec[i].char_storage + n);
  12340. }
  12341. }
  12342. }
  12343. ++state->redo_point;
  12344. num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
  12345. if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
  12346. state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
  12347. }
  12348. }
  12349. NK_INTERN struct nk_text_undo_record*
  12350. nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
  12351. {
  12352. /* any time we create a new undo record, we discard redo*/
  12353. nk_textedit_flush_redo(state);
  12354. /* if we have no free records, we have to make room,
  12355. * by sliding the existing records down */
  12356. if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  12357. nk_textedit_discard_undo(state);
  12358. /* if the characters to store won't possibly fit in the buffer,
  12359. * we can't undo */
  12360. if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
  12361. state->undo_point = 0;
  12362. state->undo_char_point = 0;
  12363. return 0;
  12364. }
  12365. /* if we don't have enough free characters in the buffer,
  12366. * we have to make room */
  12367. while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
  12368. nk_textedit_discard_undo(state);
  12369. return &state->undo_rec[state->undo_point++];
  12370. }
  12371. NK_INTERN nk_rune*
  12372. nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
  12373. int insert_len, int delete_len)
  12374. {
  12375. struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
  12376. if (r == 0)
  12377. return 0;
  12378. r->where = pos;
  12379. r->insert_length = (short) insert_len;
  12380. r->delete_length = (short) delete_len;
  12381. if (insert_len == 0) {
  12382. r->char_storage = -1;
  12383. return 0;
  12384. } else {
  12385. r->char_storage = state->undo_char_point;
  12386. state->undo_char_point = (short)(state->undo_char_point + insert_len);
  12387. return &state->undo_char[r->char_storage];
  12388. }
  12389. }
  12390. NK_API void
  12391. nk_textedit_undo(struct nk_text_edit *state)
  12392. {
  12393. struct nk_text_undo_state *s = &state->undo;
  12394. struct nk_text_undo_record u, *r;
  12395. if (s->undo_point == 0)
  12396. return;
  12397. /* we need to do two things: apply the undo record, and create a redo record */
  12398. u = s->undo_rec[s->undo_point-1];
  12399. r = &s->undo_rec[s->redo_point-1];
  12400. r->char_storage = -1;
  12401. r->insert_length = u.delete_length;
  12402. r->delete_length = u.insert_length;
  12403. r->where = u.where;
  12404. if (u.delete_length)
  12405. {
  12406. /* if the undo record says to delete characters, then the redo record will
  12407. need to re-insert the characters that get deleted, so we need to store
  12408. them.
  12409. there are three cases:
  12410. - there's enough room to store the characters
  12411. - characters stored for *redoing* don't leave room for redo
  12412. - characters stored for *undoing* don't leave room for redo
  12413. if the last is true, we have to bail */
  12414. if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
  12415. /* the undo records take up too much character space; there's no space
  12416. * to store the redo characters */
  12417. r->insert_length = 0;
  12418. } else {
  12419. int i;
  12420. /* there's definitely room to store the characters eventually */
  12421. while (s->undo_char_point + u.delete_length > s->redo_char_point) {
  12422. /* there's currently not enough room, so discard a redo record */
  12423. nk_textedit_discard_redo(s);
  12424. /* should never happen: */
  12425. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  12426. return;
  12427. }
  12428. r = &s->undo_rec[s->redo_point-1];
  12429. r->char_storage = (short)(s->redo_char_point - u.delete_length);
  12430. s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
  12431. /* now save the characters */
  12432. for (i=0; i < u.delete_length; ++i)
  12433. s->undo_char[r->char_storage + i] =
  12434. nk_str_rune_at(&state->string, u.where + i);
  12435. }
  12436. /* now we can carry out the deletion */
  12437. nk_str_delete_runes(&state->string, u.where, u.delete_length);
  12438. }
  12439. /* check type of recorded action: */
  12440. if (u.insert_length) {
  12441. /* easy case: was a deletion, so we need to insert n characters */
  12442. nk_str_insert_text_runes(&state->string, u.where,
  12443. &s->undo_char[u.char_storage], u.insert_length);
  12444. s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
  12445. }
  12446. state->cursor = (short)(u.where + u.insert_length);
  12447. s->undo_point--;
  12448. s->redo_point--;
  12449. }
  12450. NK_API void
  12451. nk_textedit_redo(struct nk_text_edit *state)
  12452. {
  12453. struct nk_text_undo_state *s = &state->undo;
  12454. struct nk_text_undo_record *u, r;
  12455. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  12456. return;
  12457. /* we need to do two things: apply the redo record, and create an undo record */
  12458. u = &s->undo_rec[s->undo_point];
  12459. r = s->undo_rec[s->redo_point];
  12460. /* we KNOW there must be room for the undo record, because the redo record
  12461. was derived from an undo record */
  12462. u->delete_length = r.insert_length;
  12463. u->insert_length = r.delete_length;
  12464. u->where = r.where;
  12465. u->char_storage = -1;
  12466. if (r.delete_length) {
  12467. /* the redo record requires us to delete characters, so the undo record
  12468. needs to store the characters */
  12469. if (s->undo_char_point + u->insert_length > s->redo_char_point) {
  12470. u->insert_length = 0;
  12471. u->delete_length = 0;
  12472. } else {
  12473. int i;
  12474. u->char_storage = s->undo_char_point;
  12475. s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
  12476. /* now save the characters */
  12477. for (i=0; i < u->insert_length; ++i) {
  12478. s->undo_char[u->char_storage + i] =
  12479. nk_str_rune_at(&state->string, u->where + i);
  12480. }
  12481. }
  12482. nk_str_delete_runes(&state->string, r.where, r.delete_length);
  12483. }
  12484. if (r.insert_length) {
  12485. /* easy case: need to insert n characters */
  12486. nk_str_insert_text_runes(&state->string, r.where,
  12487. &s->undo_char[r.char_storage], r.insert_length);
  12488. }
  12489. state->cursor = r.where + r.insert_length;
  12490. s->undo_point++;
  12491. s->redo_point++;
  12492. }
  12493. NK_INTERN void
  12494. nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
  12495. {
  12496. nk_textedit_createundo(&state->undo, where, 0, length);
  12497. }
  12498. NK_INTERN void
  12499. nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
  12500. {
  12501. int i;
  12502. nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
  12503. if (p) {
  12504. for (i=0; i < length; ++i)
  12505. p[i] = nk_str_rune_at(&state->string, where+i);
  12506. }
  12507. }
  12508. NK_INTERN void
  12509. nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
  12510. int old_length, int new_length)
  12511. {
  12512. int i;
  12513. nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
  12514. if (p) {
  12515. for (i=0; i < old_length; ++i)
  12516. p[i] = nk_str_rune_at(&state->string, where+i);
  12517. }
  12518. }
  12519. NK_INTERN void
  12520. nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
  12521. nk_plugin_filter filter)
  12522. {
  12523. /* reset the state to default */
  12524. state->undo.undo_point = 0;
  12525. state->undo.undo_char_point = 0;
  12526. state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  12527. state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  12528. state->select_end = state->select_start = 0;
  12529. state->cursor = 0;
  12530. state->has_preferred_x = 0;
  12531. state->preferred_x = 0;
  12532. state->cursor_at_end_of_line = 0;
  12533. state->initialized = 1;
  12534. state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
  12535. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  12536. state->filter = filter;
  12537. state->scrollbar = nk_vec2(0,0);
  12538. }
  12539. NK_API void
  12540. nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
  12541. {
  12542. NK_ASSERT(state);
  12543. NK_ASSERT(memory);
  12544. if (!state || !memory || !size) return;
  12545. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  12546. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  12547. nk_str_init_fixed(&state->string, memory, size);
  12548. }
  12549. NK_API void
  12550. nk_textedit_init(struct nk_text_edit *state, struct nk_allocator *alloc, nk_size size)
  12551. {
  12552. NK_ASSERT(state);
  12553. NK_ASSERT(alloc);
  12554. if (!state || !alloc) return;
  12555. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  12556. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  12557. nk_str_init(&state->string, alloc, size);
  12558. }
  12559. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  12560. NK_API void
  12561. nk_textedit_init_default(struct nk_text_edit *state)
  12562. {
  12563. NK_ASSERT(state);
  12564. if (!state) return;
  12565. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  12566. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  12567. nk_str_init_default(&state->string);
  12568. }
  12569. #endif
  12570. NK_API void
  12571. nk_textedit_select_all(struct nk_text_edit *state)
  12572. {
  12573. NK_ASSERT(state);
  12574. state->select_start = 0;
  12575. state->select_end = state->string.len;
  12576. }
  12577. NK_API void
  12578. nk_textedit_free(struct nk_text_edit *state)
  12579. {
  12580. NK_ASSERT(state);
  12581. if (!state) return;
  12582. nk_str_free(&state->string);
  12583. }
  12584. /* ===============================================================
  12585. *
  12586. * TEXT WIDGET
  12587. *
  12588. * ===============================================================*/
  12589. #define nk_widget_state_reset(s)\
  12590. if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
  12591. (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
  12592. else (*(s)) = NK_WIDGET_STATE_INACTIVE;
  12593. struct nk_text {
  12594. struct nk_vec2 padding;
  12595. struct nk_color background;
  12596. struct nk_color text;
  12597. };
  12598. NK_INTERN void
  12599. nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
  12600. const char *string, int len, const struct nk_text *t,
  12601. nk_flags a, const struct nk_user_font *f)
  12602. {
  12603. struct nk_rect label;
  12604. float text_width;
  12605. NK_ASSERT(o);
  12606. NK_ASSERT(t);
  12607. if (!o || !t) return;
  12608. b.h = NK_MAX(b.h, 2 * t->padding.y);
  12609. label.x = 0; label.w = 0;
  12610. label.y = b.y + t->padding.y;
  12611. label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
  12612. text_width = f->width(f->userdata, f->height, (const char*)string, len);
  12613. text_width += (2.0f * t->padding.x);
  12614. /* align in x-axis */
  12615. if (a & NK_TEXT_ALIGN_LEFT) {
  12616. label.x = b.x + t->padding.x;
  12617. label.w = NK_MAX(0, b.w - 2 * t->padding.x);
  12618. } else if (a & NK_TEXT_ALIGN_CENTERED) {
  12619. label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
  12620. label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
  12621. label.x = NK_MAX(b.x + t->padding.x, label.x);
  12622. label.w = NK_MIN(b.x + b.w, label.x + label.w);
  12623. if (label.w >= label.x) label.w -= label.x;
  12624. } else if (a & NK_TEXT_ALIGN_RIGHT) {
  12625. label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
  12626. label.w = (float)text_width + 2 * t->padding.x;
  12627. } else return;
  12628. /* align in y-axis */
  12629. if (a & NK_TEXT_ALIGN_MIDDLE) {
  12630. label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
  12631. label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
  12632. } else if (a & NK_TEXT_ALIGN_BOTTOM) {
  12633. label.y = b.y + b.h - f->height;
  12634. label.h = f->height;
  12635. }
  12636. nk_draw_text(o, label, (const char*)string,
  12637. len, f, t->background, t->text);
  12638. }
  12639. NK_INTERN void
  12640. nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
  12641. const char *string, int len, const struct nk_text *t,
  12642. const struct nk_user_font *f)
  12643. {
  12644. float width;
  12645. int glyphs = 0;
  12646. int fitting = 0;
  12647. int done = 0;
  12648. struct nk_rect line;
  12649. struct nk_text text;
  12650. NK_INTERN nk_rune seperator[] = {' '};
  12651. NK_ASSERT(o);
  12652. NK_ASSERT(t);
  12653. if (!o || !t) return;
  12654. text.padding = nk_vec2(0,0);
  12655. text.background = t->background;
  12656. text.text = t->text;
  12657. b.w = NK_MAX(b.w, 2 * t->padding.x);
  12658. b.h = NK_MAX(b.h, 2 * t->padding.y);
  12659. b.h = b.h - 2 * t->padding.y;
  12660. line.x = b.x + t->padding.x;
  12661. line.y = b.y + t->padding.y;
  12662. line.w = b.w - 2 * t->padding.x;
  12663. line.h = 2 * t->padding.y + f->height;
  12664. fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  12665. while (done < len) {
  12666. if (!fitting || line.y + line.h >= (b.y + b.h)) break;
  12667. nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
  12668. done += fitting;
  12669. line.y += f->height + 2 * t->padding.y;
  12670. fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  12671. }
  12672. }
  12673. /* ===============================================================
  12674. *
  12675. * BUTTON
  12676. *
  12677. * ===============================================================*/
  12678. NK_INTERN void
  12679. nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
  12680. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  12681. float border_width, const struct nk_user_font *font)
  12682. {
  12683. switch (type) {
  12684. case NK_SYMBOL_X:
  12685. case NK_SYMBOL_UNDERSCORE:
  12686. case NK_SYMBOL_PLUS:
  12687. case NK_SYMBOL_MINUS: {
  12688. /* single character text symbol */
  12689. const char *X = (type == NK_SYMBOL_X) ? "x":
  12690. (type == NK_SYMBOL_UNDERSCORE) ? "_":
  12691. (type == NK_SYMBOL_PLUS) ? "+": "-";
  12692. struct nk_text text;
  12693. text.padding = nk_vec2(0,0);
  12694. text.background = background;
  12695. text.text = foreground;
  12696. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  12697. } break;
  12698. case NK_SYMBOL_CIRCLE_SOLID:
  12699. case NK_SYMBOL_CIRCLE_OUTLINE:
  12700. case NK_SYMBOL_RECT_SOLID:
  12701. case NK_SYMBOL_RECT_OUTLINE: {
  12702. /* simple empty/filled shapes */
  12703. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  12704. nk_fill_rect(out, content, 0, foreground);
  12705. if (type == NK_SYMBOL_RECT_OUTLINE)
  12706. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  12707. } else {
  12708. nk_fill_circle(out, content, foreground);
  12709. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  12710. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  12711. }
  12712. } break;
  12713. case NK_SYMBOL_TRIANGLE_UP:
  12714. case NK_SYMBOL_TRIANGLE_DOWN:
  12715. case NK_SYMBOL_TRIANGLE_LEFT:
  12716. case NK_SYMBOL_TRIANGLE_RIGHT: {
  12717. enum nk_heading heading;
  12718. struct nk_vec2 points[3];
  12719. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  12720. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
  12721. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
  12722. nk_triangle_from_direction(points, content, 0, 0, heading);
  12723. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  12724. points[2].x, points[2].y, foreground);
  12725. } break;
  12726. default:
  12727. case NK_SYMBOL_NONE:
  12728. case NK_SYMBOL_MAX: break;
  12729. }
  12730. }
  12731. NK_INTERN int
  12732. nk_button_behavior(nk_flags *state, struct nk_rect r,
  12733. const struct nk_input *i, enum nk_button_behavior behavior)
  12734. {
  12735. int ret = 0;
  12736. nk_widget_state_reset(state);
  12737. if (!i) return 0;
  12738. if (nk_input_is_mouse_hovering_rect(i, r)) {
  12739. *state = NK_WIDGET_STATE_HOVERED;
  12740. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  12741. *state = NK_WIDGET_STATE_ACTIVE;
  12742. if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
  12743. ret = (behavior != NK_BUTTON_DEFAULT) ?
  12744. nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
  12745. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  12746. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  12747. #else
  12748. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  12749. #endif
  12750. }
  12751. }
  12752. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  12753. *state |= NK_WIDGET_STATE_ENTERED;
  12754. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  12755. *state |= NK_WIDGET_STATE_LEFT;
  12756. return ret;
  12757. }
  12758. NK_INTERN const struct nk_style_item*
  12759. nk_draw_button(struct nk_command_buffer *out,
  12760. const struct nk_rect *bounds, nk_flags state,
  12761. const struct nk_style_button *style)
  12762. {
  12763. const struct nk_style_item *background;
  12764. if (state & NK_WIDGET_STATE_HOVER)
  12765. background = &style->hover;
  12766. else if (state & NK_WIDGET_STATE_ACTIVED)
  12767. background = &style->active;
  12768. else background = &style->normal;
  12769. if (background->type == NK_STYLE_ITEM_IMAGE) {
  12770. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  12771. } else {
  12772. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  12773. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  12774. }
  12775. return background;
  12776. }
  12777. NK_INTERN int
  12778. nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
  12779. const struct nk_style_button *style, const struct nk_input *in,
  12780. enum nk_button_behavior behavior, struct nk_rect *content)
  12781. {
  12782. struct nk_rect bounds;
  12783. NK_ASSERT(style);
  12784. NK_ASSERT(state);
  12785. NK_ASSERT(out);
  12786. if (!out || !style)
  12787. return nk_false;
  12788. /* calculate button content space */
  12789. content->x = r.x + style->padding.x + style->border + style->rounding;
  12790. content->y = r.y + style->padding.y + style->border + style->rounding;
  12791. content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
  12792. content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
  12793. /* execute button behavior */
  12794. bounds.x = r.x - style->touch_padding.x;
  12795. bounds.y = r.y - style->touch_padding.y;
  12796. bounds.w = r.w + 2 * style->touch_padding.x;
  12797. bounds.h = r.h + 2 * style->touch_padding.y;
  12798. return nk_button_behavior(state, bounds, in, behavior);
  12799. }
  12800. NK_INTERN void
  12801. nk_draw_button_text(struct nk_command_buffer *out,
  12802. const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
  12803. const struct nk_style_button *style, const char *txt, int len,
  12804. nk_flags text_alignment, const struct nk_user_font *font)
  12805. {
  12806. struct nk_text text;
  12807. const struct nk_style_item *background;
  12808. background = nk_draw_button(out, bounds, state, style);
  12809. /* select correct colors/images */
  12810. if (background->type == NK_STYLE_ITEM_COLOR)
  12811. text.background = background->data.color;
  12812. else text.background = style->text_background;
  12813. if (state & NK_WIDGET_STATE_HOVER)
  12814. text.text = style->text_hover;
  12815. else if (state & NK_WIDGET_STATE_ACTIVED)
  12816. text.text = style->text_active;
  12817. else text.text = style->text_normal;
  12818. text.padding = nk_vec2(0,0);
  12819. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  12820. }
  12821. NK_INTERN int
  12822. nk_do_button_text(nk_flags *state,
  12823. struct nk_command_buffer *out, struct nk_rect bounds,
  12824. const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
  12825. const struct nk_style_button *style, const struct nk_input *in,
  12826. const struct nk_user_font *font)
  12827. {
  12828. struct nk_rect content;
  12829. int ret = nk_false;
  12830. NK_ASSERT(state);
  12831. NK_ASSERT(style);
  12832. NK_ASSERT(out);
  12833. NK_ASSERT(string);
  12834. NK_ASSERT(font);
  12835. if (!out || !style || !font || !string)
  12836. return nk_false;
  12837. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  12838. if (style->draw_begin) style->draw_begin(out, style->userdata);
  12839. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  12840. if (style->draw_end) style->draw_end(out, style->userdata);
  12841. return ret;
  12842. }
  12843. NK_INTERN void
  12844. nk_draw_button_symbol(struct nk_command_buffer *out,
  12845. const struct nk_rect *bounds, const struct nk_rect *content,
  12846. nk_flags state, const struct nk_style_button *style,
  12847. enum nk_symbol_type type, const struct nk_user_font *font)
  12848. {
  12849. struct nk_color sym, bg;
  12850. const struct nk_style_item *background;
  12851. /* select correct colors/images */
  12852. background = nk_draw_button(out, bounds, state, style);
  12853. if (background->type == NK_STYLE_ITEM_COLOR)
  12854. bg = background->data.color;
  12855. else bg = style->text_background;
  12856. if (state & NK_WIDGET_STATE_HOVER)
  12857. sym = style->text_hover;
  12858. else if (state & NK_WIDGET_STATE_ACTIVED)
  12859. sym = style->text_active;
  12860. else sym = style->text_normal;
  12861. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  12862. }
  12863. NK_INTERN int
  12864. nk_do_button_symbol(nk_flags *state,
  12865. struct nk_command_buffer *out, struct nk_rect bounds,
  12866. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  12867. const struct nk_style_button *style, const struct nk_input *in,
  12868. const struct nk_user_font *font)
  12869. {
  12870. int ret;
  12871. struct nk_rect content;
  12872. NK_ASSERT(state);
  12873. NK_ASSERT(style);
  12874. NK_ASSERT(font);
  12875. NK_ASSERT(out);
  12876. if (!out || !style || !font || !state)
  12877. return nk_false;
  12878. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  12879. if (style->draw_begin) style->draw_begin(out, style->userdata);
  12880. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  12881. if (style->draw_end) style->draw_end(out, style->userdata);
  12882. return ret;
  12883. }
  12884. NK_INTERN void
  12885. nk_draw_button_image(struct nk_command_buffer *out,
  12886. const struct nk_rect *bounds, const struct nk_rect *content,
  12887. nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
  12888. {
  12889. nk_draw_button(out, bounds, state, style);
  12890. nk_draw_image(out, *content, img, nk_white);
  12891. }
  12892. NK_INTERN int
  12893. nk_do_button_image(nk_flags *state,
  12894. struct nk_command_buffer *out, struct nk_rect bounds,
  12895. struct nk_image img, enum nk_button_behavior b,
  12896. const struct nk_style_button *style, const struct nk_input *in)
  12897. {
  12898. int ret;
  12899. struct nk_rect content;
  12900. NK_ASSERT(state);
  12901. NK_ASSERT(style);
  12902. NK_ASSERT(out);
  12903. if (!out || !style || !state)
  12904. return nk_false;
  12905. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  12906. content.x += style->image_padding.x;
  12907. content.y += style->image_padding.y;
  12908. content.w -= 2 * style->image_padding.x;
  12909. content.h -= 2 * style->image_padding.y;
  12910. if (style->draw_begin) style->draw_begin(out, style->userdata);
  12911. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  12912. if (style->draw_end) style->draw_end(out, style->userdata);
  12913. return ret;
  12914. }
  12915. NK_INTERN void
  12916. nk_draw_button_text_symbol(struct nk_command_buffer *out,
  12917. const struct nk_rect *bounds, const struct nk_rect *label,
  12918. const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
  12919. const char *str, int len, enum nk_symbol_type type,
  12920. const struct nk_user_font *font)
  12921. {
  12922. struct nk_color sym;
  12923. struct nk_text text;
  12924. const struct nk_style_item *background;
  12925. /* select correct background colors/images */
  12926. background = nk_draw_button(out, bounds, state, style);
  12927. if (background->type == NK_STYLE_ITEM_COLOR)
  12928. text.background = background->data.color;
  12929. else text.background = style->text_background;
  12930. /* select correct text colors */
  12931. if (state & NK_WIDGET_STATE_HOVER) {
  12932. sym = style->text_hover;
  12933. text.text = style->text_hover;
  12934. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  12935. sym = style->text_active;
  12936. text.text = style->text_active;
  12937. } else {
  12938. sym = style->text_normal;
  12939. text.text = style->text_normal;
  12940. }
  12941. text.padding = nk_vec2(0,0);
  12942. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  12943. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  12944. }
  12945. NK_INTERN int
  12946. nk_do_button_text_symbol(nk_flags *state,
  12947. struct nk_command_buffer *out, struct nk_rect bounds,
  12948. enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
  12949. enum nk_button_behavior behavior, const struct nk_style_button *style,
  12950. const struct nk_user_font *font, const struct nk_input *in)
  12951. {
  12952. int ret;
  12953. struct nk_rect tri = {0,0,0,0};
  12954. struct nk_rect content;
  12955. NK_ASSERT(style);
  12956. NK_ASSERT(out);
  12957. NK_ASSERT(font);
  12958. if (!out || !style || !font)
  12959. return nk_false;
  12960. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  12961. tri.y = content.y + (content.h/2) - font->height/2;
  12962. tri.w = font->height; tri.h = font->height;
  12963. if (align & NK_TEXT_ALIGN_LEFT) {
  12964. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  12965. tri.x = NK_MAX(tri.x, 0);
  12966. } else tri.x = content.x + 2 * style->padding.x;
  12967. /* draw button */
  12968. if (style->draw_begin) style->draw_begin(out, style->userdata);
  12969. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  12970. *state, style, str, len, symbol, font);
  12971. if (style->draw_end) style->draw_end(out, style->userdata);
  12972. return ret;
  12973. }
  12974. NK_INTERN void
  12975. nk_draw_button_text_image(struct nk_command_buffer *out,
  12976. const struct nk_rect *bounds, const struct nk_rect *label,
  12977. const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
  12978. const char *str, int len, const struct nk_user_font *font,
  12979. const struct nk_image *img)
  12980. {
  12981. struct nk_text text;
  12982. const struct nk_style_item *background;
  12983. background = nk_draw_button(out, bounds, state, style);
  12984. /* select correct colors */
  12985. if (background->type == NK_STYLE_ITEM_COLOR)
  12986. text.background = background->data.color;
  12987. else text.background = style->text_background;
  12988. if (state & NK_WIDGET_STATE_HOVER)
  12989. text.text = style->text_hover;
  12990. else if (state & NK_WIDGET_STATE_ACTIVED)
  12991. text.text = style->text_active;
  12992. else text.text = style->text_normal;
  12993. text.padding = nk_vec2(0,0);
  12994. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  12995. nk_draw_image(out, *image, img, nk_white);
  12996. }
  12997. NK_INTERN int
  12998. nk_do_button_text_image(nk_flags *state,
  12999. struct nk_command_buffer *out, struct nk_rect bounds,
  13000. struct nk_image img, const char* str, int len, nk_flags align,
  13001. enum nk_button_behavior behavior, const struct nk_style_button *style,
  13002. const struct nk_user_font *font, const struct nk_input *in)
  13003. {
  13004. int ret;
  13005. struct nk_rect icon;
  13006. struct nk_rect content;
  13007. NK_ASSERT(style);
  13008. NK_ASSERT(state);
  13009. NK_ASSERT(font);
  13010. NK_ASSERT(out);
  13011. if (!out || !font || !style || !str)
  13012. return nk_false;
  13013. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  13014. icon.y = bounds.y + style->padding.y;
  13015. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  13016. if (align & NK_TEXT_ALIGN_LEFT) {
  13017. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  13018. icon.x = NK_MAX(icon.x, 0);
  13019. } else icon.x = bounds.x + 2 * style->padding.x;
  13020. icon.x += style->image_padding.x;
  13021. icon.y += style->image_padding.y;
  13022. icon.w -= 2 * style->image_padding.x;
  13023. icon.h -= 2 * style->image_padding.y;
  13024. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13025. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  13026. if (style->draw_end) style->draw_end(out, style->userdata);
  13027. return ret;
  13028. }
  13029. /* ===============================================================
  13030. *
  13031. * TOGGLE
  13032. *
  13033. * ===============================================================*/
  13034. enum nk_toggle_type {
  13035. NK_TOGGLE_CHECK,
  13036. NK_TOGGLE_OPTION
  13037. };
  13038. NK_INTERN int
  13039. nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
  13040. nk_flags *state, int active)
  13041. {
  13042. nk_widget_state_reset(state);
  13043. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  13044. *state = NK_WIDGET_STATE_ACTIVE;
  13045. active = !active;
  13046. }
  13047. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  13048. *state |= NK_WIDGET_STATE_ENTERED;
  13049. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  13050. *state |= NK_WIDGET_STATE_LEFT;
  13051. return active;
  13052. }
  13053. NK_INTERN void
  13054. nk_draw_checkbox(struct nk_command_buffer *out,
  13055. nk_flags state, const struct nk_style_toggle *style, int active,
  13056. const struct nk_rect *label, const struct nk_rect *selector,
  13057. const struct nk_rect *cursors, const char *string, int len,
  13058. const struct nk_user_font *font)
  13059. {
  13060. const struct nk_style_item *background;
  13061. const struct nk_style_item *cursor;
  13062. struct nk_text text;
  13063. /* select correct colors/images */
  13064. if (state & NK_WIDGET_STATE_HOVER) {
  13065. background = &style->hover;
  13066. cursor = &style->cursor_hover;
  13067. text.text = style->text_hover;
  13068. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  13069. background = &style->hover;
  13070. cursor = &style->cursor_hover;
  13071. text.text = style->text_active;
  13072. } else {
  13073. background = &style->normal;
  13074. cursor = &style->cursor_normal;
  13075. text.text = style->text_normal;
  13076. }
  13077. /* draw background and cursor */
  13078. if (background->type == NK_STYLE_ITEM_COLOR) {
  13079. nk_fill_rect(out, *selector, 0, style->border_color);
  13080. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
  13081. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  13082. if (active) {
  13083. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  13084. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  13085. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  13086. }
  13087. text.padding.x = 0;
  13088. text.padding.y = 0;
  13089. text.background = style->text_background;
  13090. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  13091. }
  13092. NK_INTERN void
  13093. nk_draw_option(struct nk_command_buffer *out,
  13094. nk_flags state, const struct nk_style_toggle *style, int active,
  13095. const struct nk_rect *label, const struct nk_rect *selector,
  13096. const struct nk_rect *cursors, const char *string, int len,
  13097. const struct nk_user_font *font)
  13098. {
  13099. const struct nk_style_item *background;
  13100. const struct nk_style_item *cursor;
  13101. struct nk_text text;
  13102. /* select correct colors/images */
  13103. if (state & NK_WIDGET_STATE_HOVER) {
  13104. background = &style->hover;
  13105. cursor = &style->cursor_hover;
  13106. text.text = style->text_hover;
  13107. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  13108. background = &style->hover;
  13109. cursor = &style->cursor_hover;
  13110. text.text = style->text_active;
  13111. } else {
  13112. background = &style->normal;
  13113. cursor = &style->cursor_normal;
  13114. text.text = style->text_normal;
  13115. }
  13116. /* draw background and cursor */
  13117. if (background->type == NK_STYLE_ITEM_COLOR) {
  13118. nk_fill_circle(out, *selector, style->border_color);
  13119. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
  13120. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  13121. if (active) {
  13122. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  13123. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  13124. else nk_fill_circle(out, *cursors, cursor->data.color);
  13125. }
  13126. text.padding.x = 0;
  13127. text.padding.y = 0;
  13128. text.background = style->text_background;
  13129. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  13130. }
  13131. NK_INTERN int
  13132. nk_do_toggle(nk_flags *state,
  13133. struct nk_command_buffer *out, struct nk_rect r,
  13134. int *active, const char *str, int len, enum nk_toggle_type type,
  13135. const struct nk_style_toggle *style, const struct nk_input *in,
  13136. const struct nk_user_font *font)
  13137. {
  13138. int was_active;
  13139. struct nk_rect bounds;
  13140. struct nk_rect select;
  13141. struct nk_rect cursor;
  13142. struct nk_rect label;
  13143. NK_ASSERT(style);
  13144. NK_ASSERT(out);
  13145. NK_ASSERT(font);
  13146. if (!out || !style || !font || !active)
  13147. return 0;
  13148. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  13149. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  13150. /* add additional touch padding for touch screen devices */
  13151. bounds.x = r.x - style->touch_padding.x;
  13152. bounds.y = r.y - style->touch_padding.y;
  13153. bounds.w = r.w + 2 * style->touch_padding.x;
  13154. bounds.h = r.h + 2 * style->touch_padding.y;
  13155. /* calculate the selector space */
  13156. select.w = font->height;
  13157. select.h = select.w;
  13158. select.y = r.y + r.h/2.0f - select.h/2.0f;
  13159. select.x = r.x;
  13160. /* calculate the bounds of the cursor inside the selector */
  13161. cursor.x = select.x + style->padding.x + style->border;
  13162. cursor.y = select.y + style->padding.y + style->border;
  13163. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  13164. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  13165. /* label behind the selector */
  13166. label.x = select.x + select.w + style->spacing;
  13167. label.y = select.y;
  13168. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  13169. label.h = select.w;
  13170. /* update selector */
  13171. was_active = *active;
  13172. *active = nk_toggle_behavior(in, bounds, state, *active);
  13173. /* draw selector */
  13174. if (style->draw_begin)
  13175. style->draw_begin(out, style->userdata);
  13176. if (type == NK_TOGGLE_CHECK) {
  13177. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  13178. } else {
  13179. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  13180. }
  13181. if (style->draw_end)
  13182. style->draw_end(out, style->userdata);
  13183. return (was_active != *active);
  13184. }
  13185. /* ===============================================================
  13186. *
  13187. * SELECTABLE
  13188. *
  13189. * ===============================================================*/
  13190. NK_INTERN void
  13191. nk_draw_selectable(struct nk_command_buffer *out,
  13192. nk_flags state, const struct nk_style_selectable *style, int active,
  13193. const struct nk_rect *bounds, const struct nk_rect *icon, const struct nk_image *img,
  13194. const char *string, int len, nk_flags align, const struct nk_user_font *font)
  13195. {
  13196. const struct nk_style_item *background;
  13197. struct nk_text text;
  13198. text.padding = style->padding;
  13199. /* select correct colors/images */
  13200. if (!active) {
  13201. if (state & NK_WIDGET_STATE_ACTIVED) {
  13202. background = &style->pressed;
  13203. text.text = style->text_pressed;
  13204. } else if (state & NK_WIDGET_STATE_HOVER) {
  13205. background = &style->hover;
  13206. text.text = style->text_hover;
  13207. } else {
  13208. background = &style->normal;
  13209. text.text = style->text_normal;
  13210. }
  13211. } else {
  13212. if (state & NK_WIDGET_STATE_ACTIVED) {
  13213. background = &style->pressed_active;
  13214. text.text = style->text_pressed_active;
  13215. } else if (state & NK_WIDGET_STATE_HOVER) {
  13216. background = &style->hover_active;
  13217. text.text = style->text_hover_active;
  13218. } else {
  13219. background = &style->normal_active;
  13220. text.text = style->text_normal_active;
  13221. }
  13222. }
  13223. /* draw selectable background and text */
  13224. if (background->type == NK_STYLE_ITEM_IMAGE) {
  13225. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  13226. text.background = nk_rgba(0,0,0,0);
  13227. } else {
  13228. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  13229. text.background = background->data.color;
  13230. }
  13231. if (img && icon) nk_draw_image(out, *icon, img, nk_white);
  13232. nk_widget_text(out, *bounds, string, len, &text, align, font);
  13233. }
  13234. NK_INTERN int
  13235. nk_do_selectable(nk_flags *state, struct nk_command_buffer *out,
  13236. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  13237. const struct nk_style_selectable *style, const struct nk_input *in,
  13238. const struct nk_user_font *font)
  13239. {
  13240. int old_value;
  13241. struct nk_rect touch;
  13242. NK_ASSERT(state);
  13243. NK_ASSERT(out);
  13244. NK_ASSERT(str);
  13245. NK_ASSERT(len);
  13246. NK_ASSERT(value);
  13247. NK_ASSERT(style);
  13248. NK_ASSERT(font);
  13249. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  13250. old_value = *value;
  13251. /* remove padding */
  13252. touch.x = bounds.x - style->touch_padding.x;
  13253. touch.y = bounds.y - style->touch_padding.y;
  13254. touch.w = bounds.w + style->touch_padding.x * 2;
  13255. touch.h = bounds.h + style->touch_padding.y * 2;
  13256. /* update button */
  13257. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  13258. *value = !(*value);
  13259. /* draw selectable */
  13260. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13261. nk_draw_selectable(out, *state, style, *value, &bounds, 0,0, str, len, align, font);
  13262. if (style->draw_end) style->draw_end(out, style->userdata);
  13263. return old_value != *value;
  13264. }
  13265. NK_INTERN int
  13266. nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out,
  13267. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  13268. const struct nk_image *img, const struct nk_style_selectable *style,
  13269. const struct nk_input *in, const struct nk_user_font *font)
  13270. {
  13271. int old_value;
  13272. struct nk_rect touch;
  13273. struct nk_rect icon;
  13274. NK_ASSERT(state);
  13275. NK_ASSERT(out);
  13276. NK_ASSERT(str);
  13277. NK_ASSERT(len);
  13278. NK_ASSERT(value);
  13279. NK_ASSERT(style);
  13280. NK_ASSERT(font);
  13281. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  13282. old_value = *value;
  13283. /* toggle behavior */
  13284. touch.x = bounds.x - style->touch_padding.x;
  13285. touch.y = bounds.y - style->touch_padding.y;
  13286. touch.w = bounds.w + style->touch_padding.x * 2;
  13287. touch.h = bounds.h + style->touch_padding.y * 2;
  13288. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  13289. *value = !(*value);
  13290. icon.y = bounds.y + style->padding.y;
  13291. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  13292. if (align & NK_TEXT_ALIGN_LEFT) {
  13293. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  13294. icon.x = NK_MAX(icon.x, 0);
  13295. } else icon.x = bounds.x + 2 * style->padding.x;
  13296. icon.x += style->image_padding.x;
  13297. icon.y += style->image_padding.y;
  13298. icon.w -= 2 * style->image_padding.x;
  13299. icon.h -= 2 * style->image_padding.y;
  13300. /* draw selectable */
  13301. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13302. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, str, len, align, font);
  13303. if (style->draw_end) style->draw_end(out, style->userdata);
  13304. return old_value != *value;
  13305. }
  13306. /* ===============================================================
  13307. *
  13308. * SLIDER
  13309. *
  13310. * ===============================================================*/
  13311. NK_INTERN float
  13312. nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor,
  13313. struct nk_rect *visual_cursor, struct nk_input *in,
  13314. struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
  13315. float slider_step, float slider_steps)
  13316. {
  13317. int left_mouse_down;
  13318. int left_mouse_click_in_cursor;
  13319. /* check if visual cursor is being dragged */
  13320. nk_widget_state_reset(state);
  13321. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  13322. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  13323. NK_BUTTON_LEFT, *visual_cursor, nk_true);
  13324. if (left_mouse_down && left_mouse_click_in_cursor)
  13325. {
  13326. float ratio = 0;
  13327. const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
  13328. const float pxstep = bounds.w / slider_steps;
  13329. /* only update value if the next slider step is reached */
  13330. *state = NK_WIDGET_STATE_ACTIVE;
  13331. if (NK_ABS(d) >= pxstep) {
  13332. const float steps = (float)((int)(NK_ABS(d) / pxstep));
  13333. slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
  13334. slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
  13335. ratio = (slider_value - slider_min)/slider_step;
  13336. logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
  13337. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
  13338. }
  13339. }
  13340. /* slider widget state */
  13341. if (nk_input_is_mouse_hovering_rect(in, bounds))
  13342. *state = NK_WIDGET_STATE_HOVERED;
  13343. if (*state & NK_WIDGET_STATE_HOVER &&
  13344. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  13345. *state |= NK_WIDGET_STATE_ENTERED;
  13346. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  13347. *state |= NK_WIDGET_STATE_LEFT;
  13348. return slider_value;
  13349. }
  13350. NK_INTERN void
  13351. nk_draw_slider(struct nk_command_buffer *out, nk_flags state,
  13352. const struct nk_style_slider *style, const struct nk_rect *bounds,
  13353. const struct nk_rect *visual_cursor, float min, float value, float max)
  13354. {
  13355. struct nk_rect fill;
  13356. struct nk_rect bar;
  13357. const struct nk_style_item *background;
  13358. /* select correct slider images/colors */
  13359. struct nk_color bar_color;
  13360. const struct nk_style_item *cursor;
  13361. NK_UNUSED(min);
  13362. NK_UNUSED(max);
  13363. NK_UNUSED(value);
  13364. if (state & NK_WIDGET_STATE_ACTIVED) {
  13365. background = &style->active;
  13366. bar_color = style->bar_active;
  13367. cursor = &style->cursor_active;
  13368. } else if (state & NK_WIDGET_STATE_HOVER) {
  13369. background = &style->hover;
  13370. bar_color = style->bar_hover;
  13371. cursor = &style->cursor_hover;
  13372. } else {
  13373. background = &style->normal;
  13374. bar_color = style->bar_normal;
  13375. cursor = &style->cursor_normal;
  13376. }
  13377. /* calculate slider background bar */
  13378. bar.x = bounds->x;
  13379. bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
  13380. bar.w = bounds->w;
  13381. bar.h = bounds->h/6;
  13382. /* filled background bar style */
  13383. fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
  13384. fill.x = bar.x;
  13385. fill.y = bar.y;
  13386. fill.h = bar.h;
  13387. /* draw background */
  13388. if (background->type == NK_STYLE_ITEM_IMAGE) {
  13389. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  13390. } else {
  13391. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  13392. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  13393. }
  13394. /* draw slider bar */
  13395. nk_fill_rect(out, bar, style->rounding, bar_color);
  13396. nk_fill_rect(out, fill, style->rounding, style->bar_filled);
  13397. /* draw cursor */
  13398. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  13399. nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_white);
  13400. else nk_fill_circle(out, *visual_cursor, cursor->data.color);
  13401. }
  13402. NK_INTERN float
  13403. nk_do_slider(nk_flags *state,
  13404. struct nk_command_buffer *out, struct nk_rect bounds,
  13405. float min, float val, float max, float step,
  13406. const struct nk_style_slider *style, struct nk_input *in,
  13407. const struct nk_user_font *font)
  13408. {
  13409. float slider_range;
  13410. float slider_min;
  13411. float slider_max;
  13412. float slider_value;
  13413. float slider_steps;
  13414. float cursor_offset;
  13415. struct nk_rect visual_cursor;
  13416. struct nk_rect logical_cursor;
  13417. NK_ASSERT(style);
  13418. NK_ASSERT(out);
  13419. if (!out || !style)
  13420. return 0;
  13421. /* remove padding from slider bounds */
  13422. bounds.x = bounds.x + style->padding.x;
  13423. bounds.y = bounds.y + style->padding.y;
  13424. bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
  13425. bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
  13426. bounds.w -= 2 * style->padding.x;
  13427. bounds.h -= 2 * style->padding.y;
  13428. /* optional buttons */
  13429. if (style->show_buttons) {
  13430. nk_flags ws;
  13431. struct nk_rect button;
  13432. button.y = bounds.y;
  13433. button.w = bounds.h;
  13434. button.h = bounds.h;
  13435. /* decrement button */
  13436. button.x = bounds.x;
  13437. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
  13438. &style->dec_button, in, font))
  13439. val -= step;
  13440. /* increment button */
  13441. button.x = (bounds.x + bounds.w) - button.w;
  13442. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
  13443. &style->inc_button, in, font))
  13444. val += step;
  13445. bounds.x = bounds.x + button.w + style->spacing.x;
  13446. bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
  13447. }
  13448. /* remove one cursor size to support visual cursor */
  13449. bounds.x += style->cursor_size.x*0.5f;
  13450. bounds.w -= style->cursor_size.x;
  13451. /* make sure the provided values are correct */
  13452. slider_max = NK_MAX(min, max);
  13453. slider_min = NK_MIN(min, max);
  13454. slider_value = NK_CLAMP(slider_min, val, slider_max);
  13455. slider_range = slider_max - slider_min;
  13456. slider_steps = slider_range / step;
  13457. cursor_offset = (slider_value - slider_min) / step;
  13458. /* calculate cursor
  13459. Basically you have two cursors. One for visual representation and interaction
  13460. and one for updating the actual cursor value. */
  13461. logical_cursor.h = bounds.h;
  13462. logical_cursor.w = bounds.w / slider_steps;
  13463. logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
  13464. logical_cursor.y = bounds.y;
  13465. visual_cursor.h = style->cursor_size.y;
  13466. visual_cursor.w = style->cursor_size.x;
  13467. visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
  13468. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  13469. slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
  13470. in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
  13471. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  13472. /* draw slider */
  13473. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13474. nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
  13475. if (style->draw_end) style->draw_end(out, style->userdata);
  13476. return slider_value;
  13477. }
  13478. /* ===============================================================
  13479. *
  13480. * PROGRESSBAR
  13481. *
  13482. * ===============================================================*/
  13483. NK_INTERN nk_size
  13484. nk_progress_behavior(nk_flags *state, const struct nk_input *in,
  13485. struct nk_rect r, nk_size max, nk_size value, int modifiable)
  13486. {
  13487. nk_widget_state_reset(state);
  13488. if (in && modifiable && nk_input_is_mouse_hovering_rect(in, r)) {
  13489. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  13490. int left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  13491. NK_BUTTON_LEFT, r, nk_true);
  13492. if (left_mouse_down && left_mouse_click_in_cursor) {
  13493. float ratio = NK_MAX(0, (float)(in->mouse.pos.x - r.x)) / (float)r.w;
  13494. value = (nk_size)NK_MAX(0,((float)max * ratio));
  13495. *state = NK_WIDGET_STATE_ACTIVE;
  13496. } else *state = NK_WIDGET_STATE_HOVERED;
  13497. }
  13498. /* set progressbar widget state */
  13499. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
  13500. *state |= NK_WIDGET_STATE_ENTERED;
  13501. else if (nk_input_is_mouse_prev_hovering_rect(in, r))
  13502. *state |= NK_WIDGET_STATE_LEFT;
  13503. if (!max) return value;
  13504. value = NK_MIN(value, max);
  13505. return value;
  13506. }
  13507. NK_INTERN void
  13508. nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
  13509. const struct nk_style_progress *style, const struct nk_rect *bounds,
  13510. const struct nk_rect *scursor, nk_size value, nk_size max)
  13511. {
  13512. const struct nk_style_item *background;
  13513. const struct nk_style_item *cursor;
  13514. NK_UNUSED(max);
  13515. NK_UNUSED(value);
  13516. /* select correct colors/images to draw */
  13517. if (state & NK_WIDGET_STATE_ACTIVED) {
  13518. background = &style->active;
  13519. cursor = &style->cursor_active;
  13520. } else if (state & NK_WIDGET_STATE_HOVER){
  13521. background = &style->hover;
  13522. cursor = &style->cursor_hover;
  13523. } else {
  13524. background = &style->normal;
  13525. cursor = &style->cursor_normal;
  13526. }
  13527. /* draw background */
  13528. if (background->type == NK_STYLE_ITEM_COLOR) {
  13529. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  13530. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  13531. } else nk_draw_image(out, *bounds, &background->data.image, nk_white);
  13532. /* draw cursor */
  13533. if (background->type == NK_STYLE_ITEM_COLOR) {
  13534. nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
  13535. nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
  13536. } else nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
  13537. }
  13538. NK_INTERN nk_size
  13539. nk_do_progress(nk_flags *state,
  13540. struct nk_command_buffer *out, struct nk_rect bounds,
  13541. nk_size value, nk_size max, int modifiable,
  13542. const struct nk_style_progress *style, const struct nk_input *in)
  13543. {
  13544. float prog_scale;
  13545. nk_size prog_value;
  13546. struct nk_rect cursor;
  13547. NK_ASSERT(style);
  13548. NK_ASSERT(out);
  13549. if (!out || !style) return 0;
  13550. /* calculate progressbar cursor */
  13551. cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
  13552. cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
  13553. cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
  13554. prog_scale = (float)value / (float)max;
  13555. cursor.w = (bounds.w - 2) * prog_scale;
  13556. /* update progressbar */
  13557. prog_value = NK_MIN(value, max);
  13558. prog_value = nk_progress_behavior(state, in, bounds, max, prog_value, modifiable);
  13559. /* draw progressbar */
  13560. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13561. nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
  13562. if (style->draw_end) style->draw_end(out, style->userdata);
  13563. return prog_value;
  13564. }
  13565. /* ===============================================================
  13566. *
  13567. * SCROLLBAR
  13568. *
  13569. * ===============================================================*/
  13570. NK_INTERN float
  13571. nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
  13572. int has_scrolling, const struct nk_rect *scroll,
  13573. const struct nk_rect *cursor, const struct nk_rect *empty0,
  13574. const struct nk_rect *empty1, float scroll_offset,
  13575. float target, float scroll_step, enum nk_orientation o)
  13576. {
  13577. nk_flags ws = 0;
  13578. int left_mouse_down;
  13579. int left_mouse_click_in_cursor;
  13580. float scroll_delta;
  13581. nk_widget_state_reset(state);
  13582. if (!in) return scroll_offset;
  13583. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  13584. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  13585. NK_BUTTON_LEFT, *cursor, nk_true);
  13586. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  13587. *state = NK_WIDGET_STATE_HOVERED;
  13588. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
  13589. if (left_mouse_down && left_mouse_click_in_cursor) {
  13590. /* update cursor by mouse dragging */
  13591. float pixel, delta;
  13592. *state = NK_WIDGET_STATE_ACTIVE;
  13593. if (o == NK_VERTICAL) {
  13594. float cursor_y;
  13595. pixel = in->mouse.delta.y;
  13596. delta = (pixel / scroll->h) * target;
  13597. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  13598. cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
  13599. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
  13600. } else {
  13601. float cursor_x;
  13602. pixel = in->mouse.delta.x;
  13603. delta = (pixel / scroll->w) * target;
  13604. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  13605. cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
  13606. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
  13607. }
  13608. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
  13609. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  13610. /* scroll page up by click on empty space or shortcut */
  13611. if (o == NK_VERTICAL)
  13612. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  13613. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  13614. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  13615. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  13616. /* scroll page down by click on empty space or shortcut */
  13617. if (o == NK_VERTICAL)
  13618. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  13619. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  13620. } else if (has_scrolling) {
  13621. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  13622. /* update cursor by mouse scrolling */
  13623. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  13624. if (o == NK_VERTICAL)
  13625. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  13626. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  13627. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  13628. /* update cursor to the beginning */
  13629. if (o == NK_VERTICAL) scroll_offset = 0;
  13630. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  13631. /* update cursor to the end */
  13632. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  13633. }
  13634. }
  13635. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  13636. *state |= NK_WIDGET_STATE_ENTERED;
  13637. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  13638. *state |= NK_WIDGET_STATE_LEFT;
  13639. return scroll_offset;
  13640. }
  13641. NK_INTERN void
  13642. nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
  13643. const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
  13644. const struct nk_rect *scroll)
  13645. {
  13646. const struct nk_style_item *background;
  13647. const struct nk_style_item *cursor;
  13648. /* select correct colors/images to draw */
  13649. if (state & NK_WIDGET_STATE_ACTIVED) {
  13650. background = &style->active;
  13651. cursor = &style->cursor_active;
  13652. } else if (state & NK_WIDGET_STATE_HOVER) {
  13653. background = &style->hover;
  13654. cursor = &style->cursor_hover;
  13655. } else {
  13656. background = &style->normal;
  13657. cursor = &style->cursor_normal;
  13658. }
  13659. /* draw background */
  13660. if (background->type == NK_STYLE_ITEM_COLOR) {
  13661. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  13662. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  13663. } else {
  13664. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  13665. }
  13666. /* draw cursor */
  13667. if (background->type == NK_STYLE_ITEM_COLOR) {
  13668. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  13669. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  13670. } else nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  13671. }
  13672. NK_INTERN float
  13673. nk_do_scrollbarv(nk_flags *state,
  13674. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  13675. float offset, float target, float step, float button_pixel_inc,
  13676. const struct nk_style_scrollbar *style, struct nk_input *in,
  13677. const struct nk_user_font *font)
  13678. {
  13679. struct nk_rect empty_north;
  13680. struct nk_rect empty_south;
  13681. struct nk_rect cursor;
  13682. float scroll_step;
  13683. float scroll_offset;
  13684. float scroll_off;
  13685. float scroll_ratio;
  13686. NK_ASSERT(out);
  13687. NK_ASSERT(style);
  13688. NK_ASSERT(state);
  13689. if (!out || !style) return 0;
  13690. scroll.w = NK_MAX(scroll.w, 1);
  13691. scroll.h = NK_MAX(scroll.h, 0);
  13692. if (target <= scroll.h) return 0;
  13693. /* optional scrollbar buttons */
  13694. if (style->show_buttons) {
  13695. nk_flags ws;
  13696. float scroll_h;
  13697. struct nk_rect button;
  13698. button.x = scroll.x;
  13699. button.w = scroll.w;
  13700. button.h = scroll.w;
  13701. scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
  13702. scroll_step = NK_MIN(step, button_pixel_inc);
  13703. /* decrement button */
  13704. button.y = scroll.y;
  13705. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  13706. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  13707. offset = offset - scroll_step;
  13708. /* increment button */
  13709. button.y = scroll.y + scroll.h - button.h;
  13710. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  13711. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  13712. offset = offset + scroll_step;
  13713. scroll.y = scroll.y + button.h;
  13714. scroll.h = scroll_h;
  13715. }
  13716. /* calculate scrollbar constants */
  13717. scroll_step = NK_MIN(step, scroll.h);
  13718. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  13719. scroll_ratio = scroll.h / target;
  13720. scroll_off = scroll_offset / target;
  13721. /* calculate scrollbar cursor bounds */
  13722. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
  13723. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  13724. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  13725. cursor.x = scroll.x + style->border + style->padding.x;
  13726. /* calculate empty space around cursor */
  13727. empty_north.x = scroll.x;
  13728. empty_north.y = scroll.y;
  13729. empty_north.w = scroll.w;
  13730. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  13731. empty_south.x = scroll.x;
  13732. empty_south.y = cursor.y + cursor.h;
  13733. empty_south.w = scroll.w;
  13734. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  13735. /* update scrollbar */
  13736. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  13737. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  13738. scroll_off = scroll_offset / target;
  13739. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  13740. /* draw scrollbar */
  13741. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13742. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  13743. if (style->draw_end) style->draw_end(out, style->userdata);
  13744. return scroll_offset;
  13745. }
  13746. NK_INTERN float
  13747. nk_do_scrollbarh(nk_flags *state,
  13748. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  13749. float offset, float target, float step, float button_pixel_inc,
  13750. const struct nk_style_scrollbar *style, struct nk_input *in,
  13751. const struct nk_user_font *font)
  13752. {
  13753. struct nk_rect cursor;
  13754. struct nk_rect empty_west;
  13755. struct nk_rect empty_east;
  13756. float scroll_step;
  13757. float scroll_offset;
  13758. float scroll_off;
  13759. float scroll_ratio;
  13760. NK_ASSERT(out);
  13761. NK_ASSERT(style);
  13762. if (!out || !style) return 0;
  13763. /* scrollbar background */
  13764. scroll.h = NK_MAX(scroll.h, 1);
  13765. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  13766. if (target <= scroll.w) return 0;
  13767. /* optional scrollbar buttons */
  13768. if (style->show_buttons) {
  13769. nk_flags ws;
  13770. float scroll_w;
  13771. struct nk_rect button;
  13772. button.y = scroll.y;
  13773. button.w = scroll.h;
  13774. button.h = scroll.h;
  13775. scroll_w = scroll.w - 2 * button.w;
  13776. scroll_step = NK_MIN(step, button_pixel_inc);
  13777. /* decrement button */
  13778. button.x = scroll.x;
  13779. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  13780. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  13781. offset = offset - scroll_step;
  13782. /* increment button */
  13783. button.x = scroll.x + scroll.w - button.w;
  13784. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  13785. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  13786. offset = offset + scroll_step;
  13787. scroll.x = scroll.x + button.w;
  13788. scroll.w = scroll_w;
  13789. }
  13790. /* calculate scrollbar constants */
  13791. scroll_step = NK_MIN(step, scroll.w);
  13792. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  13793. scroll_ratio = scroll.w / target;
  13794. scroll_off = scroll_offset / target;
  13795. /* calculate cursor bounds */
  13796. cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
  13797. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  13798. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  13799. cursor.y = scroll.y + style->border + style->padding.y;
  13800. /* calculate empty space around cursor */
  13801. empty_west.x = scroll.x;
  13802. empty_west.y = scroll.y;
  13803. empty_west.w = cursor.x - scroll.x;
  13804. empty_west.h = scroll.h;
  13805. empty_east.x = cursor.x + cursor.w;
  13806. empty_east.y = scroll.y;
  13807. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  13808. empty_east.h = scroll.h;
  13809. /* update scrollbar */
  13810. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  13811. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  13812. scroll_off = scroll_offset / target;
  13813. cursor.x = scroll.x + (scroll_off * scroll.w);
  13814. /* draw scrollbar */
  13815. if (style->draw_begin) style->draw_begin(out, style->userdata);
  13816. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  13817. if (style->draw_end) style->draw_end(out, style->userdata);
  13818. return scroll_offset;
  13819. }
  13820. /* ===============================================================
  13821. *
  13822. * FILTER
  13823. *
  13824. * ===============================================================*/
  13825. NK_API int nk_filter_default(const struct nk_text_edit *box, nk_rune unicode)
  13826. {(void)unicode;NK_UNUSED(box);return nk_true;}
  13827. NK_API int
  13828. nk_filter_ascii(const struct nk_text_edit *box, nk_rune unicode)
  13829. {
  13830. NK_UNUSED(box);
  13831. if (unicode > 128) return nk_false;
  13832. else return nk_true;
  13833. }
  13834. NK_API int
  13835. nk_filter_float(const struct nk_text_edit *box, nk_rune unicode)
  13836. {
  13837. NK_UNUSED(box);
  13838. if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
  13839. return nk_false;
  13840. else return nk_true;
  13841. }
  13842. NK_API int
  13843. nk_filter_decimal(const struct nk_text_edit *box, nk_rune unicode)
  13844. {
  13845. NK_UNUSED(box);
  13846. if ((unicode < '0' || unicode > '9') && unicode != '-')
  13847. return nk_false;
  13848. else return nk_true;
  13849. }
  13850. NK_API int
  13851. nk_filter_hex(const struct nk_text_edit *box, nk_rune unicode)
  13852. {
  13853. NK_UNUSED(box);
  13854. if ((unicode < '0' || unicode > '9') &&
  13855. (unicode < 'a' || unicode > 'f') &&
  13856. (unicode < 'A' || unicode > 'F'))
  13857. return nk_false;
  13858. else return nk_true;
  13859. }
  13860. NK_API int
  13861. nk_filter_oct(const struct nk_text_edit *box, nk_rune unicode)
  13862. {
  13863. NK_UNUSED(box);
  13864. if (unicode < '0' || unicode > '7')
  13865. return nk_false;
  13866. else return nk_true;
  13867. }
  13868. NK_API int
  13869. nk_filter_binary(const struct nk_text_edit *box, nk_rune unicode)
  13870. {
  13871. NK_UNUSED(box);
  13872. if (unicode != '0' && unicode != '1')
  13873. return nk_false;
  13874. else return nk_true;
  13875. }
  13876. /* ===============================================================
  13877. *
  13878. * EDIT
  13879. *
  13880. * ===============================================================*/
  13881. NK_INTERN void
  13882. nk_edit_draw_text(struct nk_command_buffer *out,
  13883. const struct nk_style_edit *style, float pos_x, float pos_y,
  13884. float x_offset, const char *text, int byte_len, float row_height,
  13885. const struct nk_user_font *font, struct nk_color background,
  13886. struct nk_color foreground, int is_selected)
  13887. {
  13888. NK_ASSERT(out);
  13889. NK_ASSERT(font);
  13890. NK_ASSERT(style);
  13891. if (!text || !byte_len || !out || !style) return;
  13892. {int glyph_len = 0;
  13893. nk_rune unicode = 0;
  13894. int text_len = 0;
  13895. float line_width = 0;
  13896. float glyph_width;
  13897. const char *line = text;
  13898. float line_offset = 0;
  13899. int line_count = 0;
  13900. struct nk_text txt;
  13901. txt.padding = nk_vec2(0,0);
  13902. txt.background = background;
  13903. txt.text = foreground;
  13904. glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
  13905. if (!glyph_len) return;
  13906. while ((text_len < byte_len) && glyph_len)
  13907. {
  13908. if (unicode == '\n') {
  13909. /* new line sepeator so draw previous line */
  13910. struct nk_rect label;
  13911. label.y = pos_y + line_offset;
  13912. label.h = row_height;
  13913. label.w = line_width;
  13914. label.x = pos_x;
  13915. if (!line_count)
  13916. label.x += x_offset;
  13917. if (is_selected) /* selection needs to draw different background color */
  13918. nk_fill_rect(out, label, 0, background);
  13919. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  13920. &txt, NK_TEXT_CENTERED, font);
  13921. text_len++;
  13922. line_count++;
  13923. line_width = 0;
  13924. line = text + text_len;
  13925. line_offset += row_height;
  13926. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len-text_len));
  13927. continue;
  13928. }
  13929. if (unicode == '\r') {
  13930. text_len++;
  13931. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  13932. continue;
  13933. }
  13934. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  13935. line_width += (float)glyph_width;
  13936. text_len += glyph_len;
  13937. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  13938. continue;
  13939. }
  13940. if (line_width > 0) {
  13941. /* draw last line */
  13942. struct nk_rect label;
  13943. label.y = pos_y + line_offset;
  13944. label.h = row_height;
  13945. label.w = line_width;
  13946. label.x = pos_x;
  13947. if (!line_count)
  13948. label.x += x_offset;
  13949. if (is_selected)
  13950. nk_fill_rect(out, label, 0, background);
  13951. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  13952. &txt, NK_TEXT_LEFT, font);
  13953. }}
  13954. }
  13955. NK_INTERN nk_flags
  13956. nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
  13957. struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
  13958. struct nk_text_edit *edit, const struct nk_style_edit *style,
  13959. struct nk_input *in, const struct nk_user_font *font)
  13960. {
  13961. struct nk_rect area;
  13962. nk_flags ret = 0;
  13963. float row_height;
  13964. char prev_state = 0;
  13965. char is_hovered = 0;
  13966. char select_all = 0;
  13967. char cursor_follow = 0;
  13968. struct nk_rect old_clip;
  13969. struct nk_rect clip;
  13970. NK_ASSERT(state);
  13971. NK_ASSERT(out);
  13972. NK_ASSERT(style);
  13973. if (!state || !out || !style)
  13974. return ret;
  13975. /* visible text area calculation */
  13976. area.x = bounds.x + style->padding.x + style->border;
  13977. area.y = bounds.y + style->padding.y + style->border;
  13978. area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
  13979. area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
  13980. if (flags & NK_EDIT_MULTILINE)
  13981. area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
  13982. row_height = (flags & NK_EDIT_MULTILINE)? font->height + style->row_padding: area.h;
  13983. /* calculate clipping rectangle */
  13984. old_clip = out->clip;
  13985. nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
  13986. /* update edit state */
  13987. prev_state = (char)edit->active;
  13988. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, bounds);
  13989. if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
  13990. edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
  13991. bounds.x, bounds.y, bounds.w, bounds.h);
  13992. }
  13993. /* (de)activate text editor */
  13994. if (!prev_state && edit->active) {
  13995. const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
  13996. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE;
  13997. nk_textedit_clear_state(edit, type, filter);
  13998. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  13999. edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  14000. if (flags & NK_EDIT_AUTO_SELECT)
  14001. select_all = nk_true;
  14002. if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
  14003. edit->cursor = edit->string.len;
  14004. in = 0;
  14005. }
  14006. } else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  14007. if (flags & NK_EDIT_READ_ONLY)
  14008. edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  14009. ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
  14010. if (prev_state != edit->active)
  14011. ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
  14012. /* handle user input */
  14013. if (edit->active && in)
  14014. {
  14015. int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
  14016. const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
  14017. const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
  14018. /* mouse click handler */
  14019. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
  14020. if (select_all) {
  14021. nk_textedit_select_all(edit);
  14022. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  14023. in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
  14024. nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
  14025. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  14026. (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
  14027. nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
  14028. cursor_follow = nk_true;
  14029. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
  14030. in->mouse.buttons[NK_BUTTON_RIGHT].down) {
  14031. nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
  14032. nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
  14033. cursor_follow = nk_true;
  14034. }
  14035. {int i; /* keyboard input */
  14036. int old_mode = edit->mode;
  14037. for (i = 0; i < NK_KEY_MAX; ++i) {
  14038. if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
  14039. if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
  14040. nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
  14041. cursor_follow = nk_true;
  14042. }
  14043. }
  14044. if (old_mode != edit->mode) {
  14045. in->keyboard.text_len = 0;
  14046. }}
  14047. /* text input */
  14048. edit->filter = filter;
  14049. if (in->keyboard.text_len) {
  14050. nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
  14051. cursor_follow = nk_true;
  14052. in->keyboard.text_len = 0;
  14053. }
  14054. /* enter key handler */
  14055. if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
  14056. cursor_follow = nk_true;
  14057. if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
  14058. nk_textedit_text(edit, "\n", 1);
  14059. else if (flags & NK_EDIT_SIG_ENTER)
  14060. ret |= NK_EDIT_COMMITED;
  14061. else nk_textedit_text(edit, "\n", 1);
  14062. }
  14063. /* cut & copy handler */
  14064. {int copy= nk_input_is_key_pressed(in, NK_KEY_COPY);
  14065. int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
  14066. if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
  14067. {
  14068. int glyph_len;
  14069. nk_rune unicode;
  14070. const char *text;
  14071. int b = edit->select_start;
  14072. int e = edit->select_end;
  14073. int begin = NK_MIN(b, e);
  14074. int end = NK_MAX(b, e);
  14075. text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
  14076. if (edit->clip.copy)
  14077. edit->clip.copy(edit->clip.userdata, text, end - begin);
  14078. if (cut && !(flags & NK_EDIT_READ_ONLY)){
  14079. nk_textedit_cut(edit);
  14080. cursor_follow = nk_true;
  14081. }
  14082. }}
  14083. /* paste handler */
  14084. {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
  14085. if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
  14086. edit->clip.paste(edit->clip.userdata, edit);
  14087. cursor_follow = nk_true;
  14088. }}
  14089. /* tab handler */
  14090. {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
  14091. if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
  14092. nk_textedit_text(edit, " ", 4);
  14093. cursor_follow = nk_true;
  14094. }}
  14095. }
  14096. /* set widget state */
  14097. if (edit->active)
  14098. *state = NK_WIDGET_STATE_ACTIVE;
  14099. else nk_widget_state_reset(state);
  14100. if (is_hovered)
  14101. *state |= NK_WIDGET_STATE_HOVERED;
  14102. /* DRAW EDIT */
  14103. {const char *text = nk_str_get_const(&edit->string);
  14104. int len = nk_str_len_char(&edit->string);
  14105. {/* select background colors/images */
  14106. const struct nk_style_item *background;
  14107. if (*state & NK_WIDGET_STATE_ACTIVED)
  14108. background = &style->active;
  14109. else if (*state & NK_WIDGET_STATE_HOVER)
  14110. background = &style->hover;
  14111. else background = &style->normal;
  14112. /* draw background frame */
  14113. if (background->type == NK_STYLE_ITEM_COLOR) {
  14114. nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
  14115. nk_fill_rect(out, bounds, style->rounding, background->data.color);
  14116. } else nk_draw_image(out, bounds, &background->data.image, nk_white);}
  14117. area.w = NK_MAX(0, area.w - style->cursor_size);
  14118. if (edit->active)
  14119. {
  14120. int total_lines = 1;
  14121. struct nk_vec2 text_size = nk_vec2(0,0);
  14122. /* text pointer positions */
  14123. const char *cursor_ptr = 0;
  14124. const char *select_begin_ptr = 0;
  14125. const char *select_end_ptr = 0;
  14126. /* 2D pixel positions */
  14127. struct nk_vec2 cursor_pos = nk_vec2(0,0);
  14128. struct nk_vec2 selection_offset_start = nk_vec2(0,0);
  14129. struct nk_vec2 selection_offset_end = nk_vec2(0,0);
  14130. int selection_begin = NK_MIN(edit->select_start, edit->select_end);
  14131. int selection_end = NK_MAX(edit->select_start, edit->select_end);
  14132. /* calculate total line count + total space + cursor/selection position */
  14133. float line_width = 0.0f;
  14134. if (text && len)
  14135. {
  14136. /* utf8 encoding */
  14137. float glyph_width;
  14138. int glyph_len = 0;
  14139. nk_rune unicode = 0;
  14140. int text_len = 0;
  14141. int glyphs = 0;
  14142. int row_begin = 0;
  14143. glyph_len = nk_utf_decode(text, &unicode, len);
  14144. glyph_width = font->width(font->userdata, font->height, text, glyph_len);
  14145. line_width = 0;
  14146. /* iterate all lines */
  14147. while ((text_len < len) && glyph_len)
  14148. {
  14149. /* set cursor 2D position and line */
  14150. if (!cursor_ptr && glyphs == edit->cursor)
  14151. {
  14152. int glyph_offset;
  14153. struct nk_vec2 out_offset;
  14154. struct nk_vec2 row_size;
  14155. const char *remaining;
  14156. /* calculate 2d position */
  14157. cursor_pos.y = (float)(total_lines-1) * row_height;
  14158. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  14159. text_len-row_begin, row_height, &remaining,
  14160. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  14161. cursor_pos.x = row_size.x;
  14162. cursor_ptr = text + text_len;
  14163. }
  14164. /* set start selection 2D position and line */
  14165. if (!select_begin_ptr && edit->select_start != edit->select_end &&
  14166. glyphs == selection_begin)
  14167. {
  14168. int glyph_offset;
  14169. struct nk_vec2 out_offset;
  14170. struct nk_vec2 row_size;
  14171. const char *remaining;
  14172. /* calculate 2d position */
  14173. selection_offset_start.y = (float)(NK_MAX(total_lines-1,0)) * row_height;
  14174. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  14175. text_len-row_begin, row_height, &remaining,
  14176. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  14177. selection_offset_start.x = row_size.x;
  14178. select_begin_ptr = text + text_len;
  14179. }
  14180. /* set end selection 2D position and line */
  14181. if (!select_end_ptr && edit->select_start != edit->select_end &&
  14182. glyphs == selection_end)
  14183. {
  14184. int glyph_offset;
  14185. struct nk_vec2 out_offset;
  14186. struct nk_vec2 row_size;
  14187. const char *remaining;
  14188. /* calculate 2d position */
  14189. selection_offset_end.y = (float)(total_lines-1) * row_height;
  14190. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  14191. text_len-row_begin, row_height, &remaining,
  14192. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  14193. selection_offset_end.x = row_size.x;
  14194. select_end_ptr = text + text_len;
  14195. }
  14196. if (unicode == '\n') {
  14197. text_size.x = NK_MAX(text_size.x, line_width);
  14198. total_lines++;
  14199. line_width = 0;
  14200. text_len++;
  14201. glyphs++;
  14202. row_begin = text_len;
  14203. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  14204. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  14205. continue;
  14206. }
  14207. glyphs++;
  14208. text_len += glyph_len;
  14209. line_width += (float)glyph_width;
  14210. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  14211. glyph_width = font->width(font->userdata, font->height,
  14212. text+text_len, glyph_len);
  14213. continue;
  14214. }
  14215. text_size.y = (float)total_lines * row_height;
  14216. /* handle case when cursor is at end of text buffer */
  14217. if (!cursor_ptr && edit->cursor == edit->string.len) {
  14218. cursor_pos.x = line_width;
  14219. cursor_pos.y = text_size.y - row_height;
  14220. }
  14221. }
  14222. {
  14223. /* scrollbar */
  14224. if (cursor_follow)
  14225. {
  14226. /* update scrollbar to follow cursor */
  14227. if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
  14228. /* horizontal scroll */
  14229. const float scroll_increment = area.w * 0.25f;
  14230. if (cursor_pos.x < edit->scrollbar.x)
  14231. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
  14232. if (cursor_pos.x >= edit->scrollbar.x + area.w)
  14233. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x);
  14234. } else edit->scrollbar.x = 0;
  14235. if (flags & NK_EDIT_MULTILINE) {
  14236. /* vertical scroll */
  14237. if (cursor_pos.y < edit->scrollbar.y)
  14238. edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
  14239. if (cursor_pos.y >= edit->scrollbar.y + area.h)
  14240. edit->scrollbar.y = edit->scrollbar.y + row_height;
  14241. } else edit->scrollbar.y = 0;
  14242. }
  14243. /* scrollbar widget */
  14244. if (flags & NK_EDIT_MULTILINE)
  14245. {
  14246. nk_flags ws;
  14247. struct nk_rect scroll;
  14248. float scroll_target;
  14249. float scroll_offset;
  14250. float scroll_step;
  14251. float scroll_inc;
  14252. scroll = area;
  14253. scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
  14254. scroll.w = style->scrollbar_size.x;
  14255. scroll_offset = edit->scrollbar.y;
  14256. scroll_step = scroll.h * 0.10f;
  14257. scroll_inc = scroll.h * 0.01f;
  14258. scroll_target = text_size.y;
  14259. edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
  14260. scroll_offset, scroll_target, scroll_step, scroll_inc,
  14261. &style->scrollbar, in, font);
  14262. }
  14263. }
  14264. /* draw text */
  14265. {struct nk_color background_color;
  14266. struct nk_color text_color;
  14267. struct nk_color sel_background_color;
  14268. struct nk_color sel_text_color;
  14269. struct nk_color cursor_color;
  14270. struct nk_color cursor_text_color;
  14271. const struct nk_style_item *background;
  14272. nk_push_scissor(out, clip);
  14273. /* select correct colors to draw */
  14274. if (*state & NK_WIDGET_STATE_ACTIVED) {
  14275. background = &style->active;
  14276. text_color = style->text_active;
  14277. sel_text_color = style->selected_text_hover;
  14278. sel_background_color = style->selected_hover;
  14279. cursor_color = style->cursor_hover;
  14280. cursor_text_color = style->cursor_text_hover;
  14281. } else if (*state & NK_WIDGET_STATE_HOVER) {
  14282. background = &style->hover;
  14283. text_color = style->text_hover;
  14284. sel_text_color = style->selected_text_hover;
  14285. sel_background_color = style->selected_hover;
  14286. cursor_text_color = style->cursor_text_hover;
  14287. cursor_color = style->cursor_hover;
  14288. } else {
  14289. background = &style->normal;
  14290. text_color = style->text_normal;
  14291. sel_text_color = style->selected_text_normal;
  14292. sel_background_color = style->selected_normal;
  14293. cursor_color = style->cursor_normal;
  14294. cursor_text_color = style->cursor_text_normal;
  14295. }
  14296. if (background->type == NK_STYLE_ITEM_IMAGE)
  14297. background_color = nk_rgba(0,0,0,0);
  14298. else background_color = background->data.color;
  14299. if (edit->select_start == edit->select_end) {
  14300. /* no selection so just draw the complete text */
  14301. const char *begin = nk_str_get_const(&edit->string);
  14302. int l = nk_str_len_char(&edit->string);
  14303. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  14304. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  14305. background_color, text_color, nk_false);
  14306. } else {
  14307. /* edit has selection so draw 1-3 text chunks */
  14308. if (edit->select_start != edit->select_end && selection_begin > 0){
  14309. /* draw unselected text before selection */
  14310. const char *begin = nk_str_get_const(&edit->string);
  14311. NK_ASSERT(select_begin_ptr);
  14312. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  14313. area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
  14314. row_height, font, background_color, text_color, nk_false);
  14315. }
  14316. if (edit->select_start != edit->select_end) {
  14317. /* draw selected text */
  14318. NK_ASSERT(select_begin_ptr);
  14319. if (!select_end_ptr) {
  14320. const char *begin = nk_str_get_const(&edit->string);
  14321. select_end_ptr = begin + nk_str_len_char(&edit->string);
  14322. }
  14323. nk_edit_draw_text(out, style,
  14324. area.x - edit->scrollbar.x,
  14325. area.y + selection_offset_start.y - edit->scrollbar.y,
  14326. selection_offset_start.x,
  14327. select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
  14328. row_height, font, sel_background_color, sel_text_color, nk_true);
  14329. }
  14330. if ((edit->select_start != edit->select_end &&
  14331. selection_end < edit->string.len))
  14332. {
  14333. /* draw unselected text after selected text */
  14334. const char *begin = select_end_ptr;
  14335. const char *end = nk_str_get_const(&edit->string) +
  14336. nk_str_len_char(&edit->string);
  14337. NK_ASSERT(select_end_ptr);
  14338. nk_edit_draw_text(out, style,
  14339. area.x - edit->scrollbar.x,
  14340. area.y + selection_offset_end.y - edit->scrollbar.y,
  14341. selection_offset_end.x,
  14342. begin, (int)(end - begin), row_height, font,
  14343. background_color, text_color, nk_true);
  14344. }
  14345. }
  14346. /* cursor */
  14347. if (edit->select_start == edit->select_end)
  14348. {
  14349. if (edit->cursor >= nk_str_len(&edit->string) ||
  14350. (cursor_ptr && *cursor_ptr == '\n')) {
  14351. /* draw cursor at end of line */
  14352. struct nk_rect cursor;
  14353. cursor.w = style->cursor_size;
  14354. cursor.h = font->height;
  14355. cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
  14356. cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
  14357. cursor.y -= edit->scrollbar.y;
  14358. nk_fill_rect(out, cursor, 0, cursor_color);
  14359. } else {
  14360. /* draw cursor inside text */
  14361. int glyph_len;
  14362. struct nk_rect label;
  14363. struct nk_text txt;
  14364. nk_rune unicode;
  14365. NK_ASSERT(cursor_ptr);
  14366. glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
  14367. label.x = area.x + cursor_pos.x - edit->scrollbar.x;
  14368. label.y = area.y + cursor_pos.y - edit->scrollbar.y;
  14369. label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
  14370. label.h = row_height;
  14371. txt.padding = nk_vec2(0,0);
  14372. txt.background = cursor_color;;
  14373. txt.text = cursor_text_color;
  14374. nk_fill_rect(out, label, 0, cursor_color);
  14375. nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
  14376. }
  14377. }}
  14378. } else {
  14379. /* not active so just draw text */
  14380. int l = nk_str_len_char(&edit->string);
  14381. const char *begin = nk_str_get_const(&edit->string);
  14382. const struct nk_style_item *background;
  14383. struct nk_color background_color;
  14384. struct nk_color text_color;
  14385. nk_push_scissor(out, clip);
  14386. if (*state & NK_WIDGET_STATE_ACTIVED) {
  14387. background = &style->active;
  14388. text_color = style->text_active;
  14389. } else if (*state & NK_WIDGET_STATE_HOVER) {
  14390. background = &style->hover;
  14391. text_color = style->text_hover;
  14392. } else {
  14393. background = &style->normal;
  14394. text_color = style->text_normal;
  14395. }
  14396. if (background->type == NK_STYLE_ITEM_IMAGE)
  14397. background_color = nk_rgba(0,0,0,0);
  14398. else background_color = background->data.color;
  14399. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  14400. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  14401. background_color, text_color, nk_false);
  14402. }
  14403. nk_push_scissor(out, old_clip);}
  14404. return ret;
  14405. }
  14406. /* ===============================================================
  14407. *
  14408. * PROPERTY
  14409. *
  14410. * ===============================================================*/
  14411. enum nk_property_status {
  14412. NK_PROPERTY_DEFAULT,
  14413. NK_PROPERTY_EDIT,
  14414. NK_PROPERTY_DRAG
  14415. };
  14416. enum nk_property_filter {
  14417. NK_FILTER_INT,
  14418. NK_FILTER_FLOAT
  14419. };
  14420. enum nk_property_kind {
  14421. NK_PROPERTY_INT,
  14422. NK_PROPERTY_FLOAT,
  14423. NK_PROPERTY_DOUBLE
  14424. };
  14425. union nk_property {
  14426. int i;
  14427. float f;
  14428. double d;
  14429. };
  14430. struct nk_property_variant {
  14431. enum nk_property_kind kind;
  14432. union nk_property value;
  14433. union nk_property min_value;
  14434. union nk_property max_value;
  14435. union nk_property step;
  14436. };
  14437. NK_INTERN void
  14438. nk_drag_behavior(nk_flags *state, const struct nk_input *in,
  14439. struct nk_rect drag, struct nk_property_variant *variant,
  14440. float inc_per_pixel)
  14441. {
  14442. int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  14443. int left_mouse_click_in_cursor = in &&
  14444. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
  14445. nk_widget_state_reset(state);
  14446. if (nk_input_is_mouse_hovering_rect(in, drag))
  14447. *state = NK_WIDGET_STATE_HOVERED;
  14448. if (left_mouse_down && left_mouse_click_in_cursor) {
  14449. float delta, pixels;
  14450. pixels = in->mouse.delta.x;
  14451. delta = pixels * inc_per_pixel;
  14452. switch (variant->kind) {
  14453. default: break;
  14454. case NK_PROPERTY_INT:
  14455. variant->value.i = variant->value.i + (int)delta;
  14456. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  14457. break;
  14458. case NK_PROPERTY_FLOAT:
  14459. variant->value.f = variant->value.f + (float)delta;
  14460. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  14461. break;
  14462. case NK_PROPERTY_DOUBLE:
  14463. variant->value.d = variant->value.d + (double)delta;
  14464. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  14465. break;
  14466. }
  14467. *state = NK_WIDGET_STATE_ACTIVE;
  14468. }
  14469. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
  14470. *state |= NK_WIDGET_STATE_ENTERED;
  14471. else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
  14472. *state |= NK_WIDGET_STATE_LEFT;
  14473. }
  14474. NK_INTERN void
  14475. nk_property_behavior(nk_flags *ws, const struct nk_input *in,
  14476. struct nk_rect property, struct nk_rect label, struct nk_rect edit,
  14477. struct nk_rect empty, int *state, struct nk_property_variant *variant,
  14478. float inc_per_pixel)
  14479. {
  14480. if (in && *state == NK_PROPERTY_DEFAULT) {
  14481. if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
  14482. *state = NK_PROPERTY_EDIT;
  14483. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
  14484. *state = NK_PROPERTY_DRAG;
  14485. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
  14486. *state = NK_PROPERTY_DRAG;
  14487. }
  14488. if (*state == NK_PROPERTY_DRAG) {
  14489. nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
  14490. if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
  14491. }
  14492. }
  14493. NK_INTERN void
  14494. nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style,
  14495. const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state,
  14496. const char *name, int len, const struct nk_user_font *font)
  14497. {
  14498. struct nk_text text;
  14499. const struct nk_style_item *background;
  14500. /* select correct background and text color */
  14501. if (state & NK_WIDGET_STATE_ACTIVED) {
  14502. background = &style->active;
  14503. text.text = style->label_active;
  14504. } else if (state & NK_WIDGET_STATE_HOVER) {
  14505. background = &style->hover;
  14506. text.text = style->label_hover;
  14507. } else {
  14508. background = &style->normal;
  14509. text.text = style->label_normal;
  14510. }
  14511. /* draw background */
  14512. if (background->type == NK_STYLE_ITEM_IMAGE) {
  14513. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  14514. text.background = nk_rgba(0,0,0,0);
  14515. } else {
  14516. text.background = background->data.color;
  14517. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  14518. nk_stroke_rect(out, *bounds, style->rounding, style->border, background->data.color);
  14519. }
  14520. /* draw label */
  14521. text.padding = nk_vec2(0,0);
  14522. nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
  14523. }
  14524. NK_INTERN void
  14525. nk_do_property(nk_flags *ws,
  14526. struct nk_command_buffer *out, struct nk_rect property,
  14527. const char *name, struct nk_property_variant *variant,
  14528. float inc_per_pixel, char *buffer, int *len,
  14529. int *state, int *cursor, int *select_begin, int *select_end,
  14530. const struct nk_style_property *style,
  14531. enum nk_property_filter filter, struct nk_input *in,
  14532. const struct nk_user_font *font, struct nk_text_edit *text_edit,
  14533. enum nk_button_behavior behavior)
  14534. {
  14535. const nk_plugin_filter filters[] = {
  14536. nk_filter_decimal,
  14537. nk_filter_float
  14538. };
  14539. int active, old;
  14540. int num_len, name_len;
  14541. char string[NK_MAX_NUMBER_BUFFER];
  14542. float size;
  14543. char *dst = 0;
  14544. int *length;
  14545. struct nk_rect left;
  14546. struct nk_rect right;
  14547. struct nk_rect label;
  14548. struct nk_rect edit;
  14549. struct nk_rect empty;
  14550. /* left decrement button */
  14551. left.h = font->height/2;
  14552. left.w = left.h;
  14553. left.x = property.x + style->border + style->padding.x;
  14554. left.y = property.y + style->border + property.h/2.0f - left.h/2;
  14555. /* text label */
  14556. name_len = nk_strlen(name);
  14557. size = font->width(font->userdata, font->height, name, name_len);
  14558. label.x = left.x + left.w + style->padding.x;
  14559. label.w = (float)size + 2 * style->padding.x;
  14560. label.y = property.y + style->border + style->padding.y;
  14561. label.h = property.h - (2 * style->border + 2 * style->padding.y);
  14562. /* right increment button */
  14563. right.y = left.y;
  14564. right.w = left.w;
  14565. right.h = left.h;
  14566. right.x = property.x + property.w - (right.w + style->padding.x);
  14567. /* edit */
  14568. if (*state == NK_PROPERTY_EDIT) {
  14569. size = font->width(font->userdata, font->height, buffer, *len);
  14570. size += style->edit.cursor_size;
  14571. length = len;
  14572. dst = buffer;
  14573. } else {
  14574. switch (variant->kind) {
  14575. default: break;
  14576. case NK_PROPERTY_INT:
  14577. nk_itoa(string, variant->value.i);
  14578. num_len = nk_strlen(string);
  14579. break;
  14580. case NK_PROPERTY_FLOAT:
  14581. nk_dtoa(string, (double)variant->value.f);
  14582. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  14583. break;
  14584. case NK_PROPERTY_DOUBLE:
  14585. nk_dtoa(string, variant->value.d);
  14586. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  14587. break;
  14588. }
  14589. size = font->width(font->userdata, font->height, string, num_len);
  14590. dst = string;
  14591. length = &num_len;
  14592. }
  14593. edit.w = (float)size + 2 * style->padding.x;
  14594. edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
  14595. edit.x = right.x - (edit.w + style->padding.x);
  14596. edit.y = property.y + style->border;
  14597. edit.h = property.h - (2 * style->border);
  14598. /* empty left space activator */
  14599. empty.w = edit.x - (label.x + label.w);
  14600. empty.x = label.x + label.w;
  14601. empty.y = property.y;
  14602. empty.h = property.h;
  14603. /* update property */
  14604. old = (*state == NK_PROPERTY_EDIT);
  14605. nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
  14606. /* draw property */
  14607. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14608. nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
  14609. if (style->draw_end) style->draw_end(out, style->userdata);
  14610. /* execute right button */
  14611. if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
  14612. switch (variant->kind) {
  14613. default: break;
  14614. case NK_PROPERTY_INT:
  14615. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
  14616. case NK_PROPERTY_FLOAT:
  14617. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
  14618. case NK_PROPERTY_DOUBLE:
  14619. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
  14620. }
  14621. }
  14622. /* execute left button */
  14623. if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
  14624. switch (variant->kind) {
  14625. default: break;
  14626. case NK_PROPERTY_INT:
  14627. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
  14628. case NK_PROPERTY_FLOAT:
  14629. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
  14630. case NK_PROPERTY_DOUBLE:
  14631. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
  14632. }
  14633. }
  14634. if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
  14635. /* property has been activated so setup buffer */
  14636. NK_MEMCPY(buffer, dst, (nk_size)*length);
  14637. *cursor = nk_utf_len(buffer, *length);
  14638. *len = *length;
  14639. length = len;
  14640. dst = buffer;
  14641. active = 0;
  14642. } else active = (*state == NK_PROPERTY_EDIT);
  14643. /* execute and run text edit field */
  14644. nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
  14645. text_edit->active = (unsigned char)active;
  14646. text_edit->string.len = *length;
  14647. text_edit->cursor = NK_CLAMP(0, *cursor, *length);
  14648. text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
  14649. text_edit->select_end = NK_CLAMP(0,*select_end, *length);
  14650. text_edit->string.buffer.allocated = (nk_size)*length;
  14651. text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
  14652. text_edit->string.buffer.memory.ptr = dst;
  14653. text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
  14654. text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  14655. nk_do_edit(ws, out, edit, NK_EDIT_FIELD|NK_EDIT_AUTO_SELECT,
  14656. filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
  14657. *length = text_edit->string.len;
  14658. *cursor = text_edit->cursor;
  14659. *select_begin = text_edit->select_start;
  14660. *select_end = text_edit->select_end;
  14661. if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
  14662. text_edit->active = nk_false;
  14663. if (active && !text_edit->active) {
  14664. /* property is now not active so convert edit text to value*/
  14665. *state = NK_PROPERTY_DEFAULT;
  14666. buffer[*len] = '\0';
  14667. switch (variant->kind) {
  14668. default: break;
  14669. case NK_PROPERTY_INT:
  14670. variant->value.i = nk_strtoi(buffer, 0);
  14671. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  14672. break;
  14673. case NK_PROPERTY_FLOAT:
  14674. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  14675. variant->value.f = nk_strtof(buffer, 0);
  14676. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  14677. break;
  14678. case NK_PROPERTY_DOUBLE:
  14679. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  14680. variant->value.d = nk_strtod(buffer, 0);
  14681. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  14682. break;
  14683. }
  14684. }
  14685. }
  14686. /* ===============================================================
  14687. *
  14688. * COLOR PICKER
  14689. *
  14690. * ===============================================================*/
  14691. NK_INTERN int
  14692. nk_color_picker_behavior(nk_flags *state,
  14693. const struct nk_rect *bounds, const struct nk_rect *matrix,
  14694. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  14695. struct nk_color *color, const struct nk_input *in)
  14696. {
  14697. float hsva[4];
  14698. int value_changed = 0;
  14699. int hsv_changed = 0;
  14700. NK_ASSERT(state);
  14701. NK_ASSERT(matrix);
  14702. NK_ASSERT(hue_bar);
  14703. NK_ASSERT(color);
  14704. /* color matrix */
  14705. nk_color_hsva_fv(hsva, *color);
  14706. if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
  14707. hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
  14708. hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
  14709. value_changed = hsv_changed = 1;
  14710. }
  14711. /* hue bar */
  14712. if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
  14713. hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
  14714. value_changed = hsv_changed = 1;
  14715. }
  14716. /* alpha bar */
  14717. if (alpha_bar) {
  14718. if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
  14719. hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
  14720. value_changed = 1;
  14721. }
  14722. }
  14723. nk_widget_state_reset(state);
  14724. if (hsv_changed) {
  14725. *color = nk_hsva_fv(hsva);
  14726. *state = NK_WIDGET_STATE_ACTIVE;
  14727. }
  14728. if (value_changed) {
  14729. color->a = (nk_byte)(hsva[3] * 255.0f);
  14730. *state = NK_WIDGET_STATE_ACTIVE;
  14731. }
  14732. /* set color picker widget state */
  14733. if (nk_input_is_mouse_hovering_rect(in, *bounds))
  14734. *state = NK_WIDGET_STATE_HOVERED;
  14735. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  14736. *state |= NK_WIDGET_STATE_ENTERED;
  14737. else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  14738. *state |= NK_WIDGET_STATE_LEFT;
  14739. return value_changed;
  14740. }
  14741. NK_INTERN void
  14742. nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
  14743. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  14744. struct nk_color color)
  14745. {
  14746. NK_STORAGE const struct nk_color black = {0,0,0,255};
  14747. NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
  14748. NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
  14749. const float crosshair_size = 7.0f;
  14750. struct nk_color temp;
  14751. float hsva[4];
  14752. float line_y;
  14753. int i;
  14754. NK_ASSERT(o);
  14755. NK_ASSERT(matrix);
  14756. NK_ASSERT(hue_bar);
  14757. /* draw hue bar */
  14758. nk_color_hsv_fv(hsva, color);
  14759. for (i = 0; i < 6; ++i) {
  14760. NK_GLOBAL const struct nk_color hue_colors[] = {
  14761. {255, 0, 0, 255},
  14762. {255,255,0,255},
  14763. {0,255,0,255},
  14764. {0, 255,255,255},
  14765. {0,0,255,255},
  14766. {255, 0, 255, 255},
  14767. {255, 0, 0, 255}
  14768. };
  14769. nk_fill_rect_multi_color(o,
  14770. nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
  14771. hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
  14772. hue_colors[i+1], hue_colors[i+1]);
  14773. }
  14774. line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
  14775. nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
  14776. line_y, 1, nk_rgb(255,255,255));
  14777. /* draw alpha bar */
  14778. if (alpha_bar) {
  14779. float alpha = NK_SATURATE((float)color.a/255.0f);
  14780. line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
  14781. nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
  14782. nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
  14783. line_y, 1, nk_rgb(255,255,255));
  14784. }
  14785. /* draw color matrix */
  14786. temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
  14787. nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
  14788. nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
  14789. /* draw cross-hair */
  14790. {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
  14791. p.x = (float)(int)(matrix->x + S * matrix->w);
  14792. p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
  14793. nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
  14794. nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
  14795. nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
  14796. nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
  14797. }
  14798. NK_INTERN int
  14799. nk_do_color_picker(nk_flags *state,
  14800. struct nk_command_buffer *out, struct nk_color *color,
  14801. enum nk_color_format fmt, struct nk_rect bounds,
  14802. struct nk_vec2 padding, const struct nk_input *in,
  14803. const struct nk_user_font *font)
  14804. {
  14805. int ret = 0;
  14806. struct nk_rect matrix;
  14807. struct nk_rect hue_bar;
  14808. struct nk_rect alpha_bar;
  14809. float bar_w;
  14810. NK_ASSERT(out);
  14811. NK_ASSERT(color);
  14812. NK_ASSERT(state);
  14813. NK_ASSERT(font);
  14814. if (!out || !color || !state || !font)
  14815. return ret;
  14816. bar_w = font->height;
  14817. bounds.x += padding.x;
  14818. bounds.y += padding.x;
  14819. bounds.w -= 2 * padding.x;
  14820. bounds.h -= 2 * padding.y;
  14821. matrix.x = bounds.x;
  14822. matrix.y = bounds.y;
  14823. matrix.h = bounds.h;
  14824. matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
  14825. hue_bar.w = bar_w;
  14826. hue_bar.y = bounds.y;
  14827. hue_bar.h = matrix.h;
  14828. hue_bar.x = matrix.x + matrix.w + padding.x;
  14829. alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
  14830. alpha_bar.y = bounds.y;
  14831. alpha_bar.w = bar_w;
  14832. alpha_bar.h = matrix.h;
  14833. ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
  14834. (fmt == NK_RGBA) ? &alpha_bar:0, color, in);
  14835. nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *color);
  14836. return ret;
  14837. }
  14838. /* ==============================================================
  14839. *
  14840. * STYLE
  14841. *
  14842. * ===============================================================*/
  14843. NK_API void nk_style_default(struct nk_context *ctx){nk_style_from_table(ctx, 0);}
  14844. #define NK_COLOR_MAP(NK_COLOR)\
  14845. NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
  14846. NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
  14847. NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
  14848. NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
  14849. NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
  14850. NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
  14851. NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
  14852. NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
  14853. NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
  14854. NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
  14855. NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
  14856. NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
  14857. NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
  14858. NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
  14859. NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
  14860. NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
  14861. NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
  14862. NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
  14863. NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
  14864. NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
  14865. NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
  14866. NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
  14867. NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT,255, 0, 0, 255) \
  14868. NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
  14869. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
  14870. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER,120,120,120,255) \
  14871. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,150,150,150,255) \
  14872. NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255)
  14873. NK_GLOBAL const struct nk_color
  14874. nk_default_color_style[NK_COLOR_COUNT] = {
  14875. #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
  14876. NK_COLOR_MAP(NK_COLOR)
  14877. #undef NK_COLOR
  14878. };
  14879. NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = {
  14880. #define NK_COLOR(a,b,c,d,e) #a,
  14881. NK_COLOR_MAP(NK_COLOR)
  14882. #undef NK_COLOR
  14883. };
  14884. NK_API const char *nk_style_get_color_by_name(enum nk_style_colors c)
  14885. {return nk_color_names[c];}
  14886. NK_API struct nk_style_item nk_style_item_image(struct nk_image img)
  14887. {struct nk_style_item i; i.type = NK_STYLE_ITEM_IMAGE; i.data.image = img; return i;}
  14888. NK_API struct nk_style_item nk_style_item_color(struct nk_color col)
  14889. {struct nk_style_item i; i.type = NK_STYLE_ITEM_COLOR; i.data.color = col; return i;}
  14890. NK_API struct nk_style_item nk_style_item_hide(void)
  14891. {struct nk_style_item i; i.type = NK_STYLE_ITEM_COLOR; i.data.color = nk_rgba(0,0,0,0); return i;}
  14892. NK_API void
  14893. nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
  14894. {
  14895. struct nk_style *style;
  14896. struct nk_style_text *text;
  14897. struct nk_style_button *button;
  14898. struct nk_style_toggle *toggle;
  14899. struct nk_style_selectable *select;
  14900. struct nk_style_slider *slider;
  14901. struct nk_style_progress *prog;
  14902. struct nk_style_scrollbar *scroll;
  14903. struct nk_style_edit *edit;
  14904. struct nk_style_property *property;
  14905. struct nk_style_combo *combo;
  14906. struct nk_style_chart *chart;
  14907. struct nk_style_tab *tab;
  14908. struct nk_style_window *win;
  14909. NK_ASSERT(ctx);
  14910. if (!ctx) return;
  14911. style = &ctx->style;
  14912. table = (!table) ? nk_default_color_style: table;
  14913. /* default text */
  14914. text = &style->text;
  14915. text->color = table[NK_COLOR_TEXT];
  14916. text->padding = nk_vec2(0,0);
  14917. /* default button */
  14918. button = &style->button;
  14919. nk_zero_struct(*button);
  14920. button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
  14921. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  14922. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  14923. button->border_color = table[NK_COLOR_BORDER];
  14924. button->text_background = table[NK_COLOR_BUTTON];
  14925. button->text_normal = table[NK_COLOR_TEXT];
  14926. button->text_hover = table[NK_COLOR_TEXT];
  14927. button->text_active = table[NK_COLOR_TEXT];
  14928. button->padding = nk_vec2(2.0f,2.0f);
  14929. button->image_padding = nk_vec2(0.0f,0.0f);
  14930. button->touch_padding = nk_vec2(0.0f, 0.0f);
  14931. button->userdata = nk_handle_ptr(0);
  14932. button->text_alignment = NK_TEXT_CENTERED;
  14933. button->border = 1.0f;
  14934. button->rounding = 4.0f;
  14935. button->draw_begin = 0;
  14936. button->draw_end = 0;
  14937. /* contextual button */
  14938. button = &style->contextual_button;
  14939. nk_zero_struct(*button);
  14940. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  14941. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  14942. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  14943. button->border_color = table[NK_COLOR_WINDOW];
  14944. button->text_background = table[NK_COLOR_WINDOW];
  14945. button->text_normal = table[NK_COLOR_TEXT];
  14946. button->text_hover = table[NK_COLOR_TEXT];
  14947. button->text_active = table[NK_COLOR_TEXT];
  14948. button->padding = nk_vec2(2.0f,2.0f);
  14949. button->touch_padding = nk_vec2(0.0f,0.0f);
  14950. button->userdata = nk_handle_ptr(0);
  14951. button->text_alignment = NK_TEXT_CENTERED;
  14952. button->border = 0.0f;
  14953. button->rounding = 0.0f;
  14954. button->draw_begin = 0;
  14955. button->draw_end = 0;
  14956. /* menu button */
  14957. button = &style->menu_button;
  14958. nk_zero_struct(*button);
  14959. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  14960. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  14961. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  14962. button->border_color = table[NK_COLOR_WINDOW];
  14963. button->text_background = table[NK_COLOR_WINDOW];
  14964. button->text_normal = table[NK_COLOR_TEXT];
  14965. button->text_hover = table[NK_COLOR_TEXT];
  14966. button->text_active = table[NK_COLOR_TEXT];
  14967. button->padding = nk_vec2(2.0f,2.0f);
  14968. button->touch_padding = nk_vec2(0.0f,0.0f);
  14969. button->userdata = nk_handle_ptr(0);
  14970. button->text_alignment = NK_TEXT_CENTERED;
  14971. button->border = 0.0f;
  14972. button->rounding = 1.0f;
  14973. button->draw_begin = 0;
  14974. button->draw_end = 0;
  14975. /* checkbox toggle */
  14976. toggle = &style->checkbox;
  14977. nk_zero_struct(*toggle);
  14978. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  14979. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  14980. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  14981. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  14982. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  14983. toggle->userdata = nk_handle_ptr(0);
  14984. toggle->text_background = table[NK_COLOR_WINDOW];
  14985. toggle->text_normal = table[NK_COLOR_TEXT];
  14986. toggle->text_hover = table[NK_COLOR_TEXT];
  14987. toggle->text_active = table[NK_COLOR_TEXT];
  14988. toggle->padding = nk_vec2(2.0f, 2.0f);
  14989. toggle->touch_padding = nk_vec2(0,0);
  14990. toggle->border_color = nk_rgba(0,0,0,0);
  14991. toggle->border = 0.0f;
  14992. toggle->spacing = 4;
  14993. /* option toggle */
  14994. toggle = &style->option;
  14995. nk_zero_struct(*toggle);
  14996. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  14997. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  14998. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  14999. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  15000. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  15001. toggle->userdata = nk_handle_ptr(0);
  15002. toggle->text_background = table[NK_COLOR_WINDOW];
  15003. toggle->text_normal = table[NK_COLOR_TEXT];
  15004. toggle->text_hover = table[NK_COLOR_TEXT];
  15005. toggle->text_active = table[NK_COLOR_TEXT];
  15006. toggle->padding = nk_vec2(3.0f, 3.0f);
  15007. toggle->touch_padding = nk_vec2(0,0);
  15008. toggle->border_color = nk_rgba(0,0,0,0);
  15009. toggle->border = 0.0f;
  15010. toggle->spacing = 4;
  15011. /* selectable */
  15012. select = &style->selectable;
  15013. nk_zero_struct(*select);
  15014. select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
  15015. select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
  15016. select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
  15017. select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  15018. select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  15019. select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  15020. select->text_normal = table[NK_COLOR_TEXT];
  15021. select->text_hover = table[NK_COLOR_TEXT];
  15022. select->text_pressed = table[NK_COLOR_TEXT];
  15023. select->text_normal_active = table[NK_COLOR_TEXT];
  15024. select->text_hover_active = table[NK_COLOR_TEXT];
  15025. select->text_pressed_active = table[NK_COLOR_TEXT];
  15026. select->padding = nk_vec2(2.0f,2.0f);
  15027. select->touch_padding = nk_vec2(0,0);
  15028. select->userdata = nk_handle_ptr(0);
  15029. select->rounding = 0.0f;
  15030. select->draw_begin = 0;
  15031. select->draw_end = 0;
  15032. /* slider */
  15033. slider = &style->slider;
  15034. nk_zero_struct(*slider);
  15035. slider->normal = nk_style_item_hide();
  15036. slider->hover = nk_style_item_hide();
  15037. slider->active = nk_style_item_hide();
  15038. slider->bar_normal = table[NK_COLOR_SLIDER];
  15039. slider->bar_hover = table[NK_COLOR_SLIDER];
  15040. slider->bar_active = table[NK_COLOR_SLIDER];
  15041. slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
  15042. slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  15043. slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  15044. slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  15045. slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
  15046. slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
  15047. slider->cursor_size = nk_vec2(16,16);
  15048. slider->padding = nk_vec2(2,2);
  15049. slider->spacing = nk_vec2(2,2);
  15050. slider->userdata = nk_handle_ptr(0);
  15051. slider->show_buttons = nk_false;
  15052. slider->bar_height = 8;
  15053. slider->rounding = 0;
  15054. slider->draw_begin = 0;
  15055. slider->draw_end = 0;
  15056. /* slider buttons */
  15057. button = &style->slider.inc_button;
  15058. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  15059. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  15060. button->active = nk_style_item_color(nk_rgb(44,44,44));
  15061. button->border_color = nk_rgb(65,65,65);
  15062. button->text_background = nk_rgb(40,40,40);
  15063. button->text_normal = nk_rgb(175,175,175);
  15064. button->text_hover = nk_rgb(175,175,175);
  15065. button->text_active = nk_rgb(175,175,175);
  15066. button->padding = nk_vec2(8.0f,8.0f);
  15067. button->touch_padding = nk_vec2(0.0f,0.0f);
  15068. button->userdata = nk_handle_ptr(0);
  15069. button->text_alignment = NK_TEXT_CENTERED;
  15070. button->border = 1.0f;
  15071. button->rounding = 0.0f;
  15072. button->draw_begin = 0;
  15073. button->draw_end = 0;
  15074. style->slider.dec_button = style->slider.inc_button;
  15075. /* progressbar */
  15076. prog = &style->progress;
  15077. nk_zero_struct(*prog);
  15078. prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
  15079. prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
  15080. prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
  15081. prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  15082. prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  15083. prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  15084. prog->border_color = nk_rgba(0,0,0,0);
  15085. prog->cursor_border_color = nk_rgba(0,0,0,0);
  15086. prog->userdata = nk_handle_ptr(0);
  15087. prog->padding = nk_vec2(4,4);
  15088. prog->rounding = 0;
  15089. prog->border = 0;
  15090. prog->cursor_rounding = 0;
  15091. prog->cursor_border = 0;
  15092. prog->draw_begin = 0;
  15093. prog->draw_end = 0;
  15094. /* scrollbars */
  15095. scroll = &style->scrollh;
  15096. nk_zero_struct(*scroll);
  15097. scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  15098. scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  15099. scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  15100. scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
  15101. scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
  15102. scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
  15103. scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
  15104. scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
  15105. scroll->userdata = nk_handle_ptr(0);
  15106. scroll->border_color = table[NK_COLOR_SCROLLBAR];
  15107. scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
  15108. scroll->padding = nk_vec2(0,0);
  15109. scroll->show_buttons = nk_false;
  15110. scroll->border = 0;
  15111. scroll->rounding = 0;
  15112. scroll->border_cursor = 0;
  15113. scroll->rounding_cursor = 0;
  15114. scroll->draw_begin = 0;
  15115. scroll->draw_end = 0;
  15116. style->scrollv = style->scrollh;
  15117. /* scrollbars buttons */
  15118. button = &style->scrollh.inc_button;
  15119. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  15120. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  15121. button->active = nk_style_item_color(nk_rgb(44,44,44));
  15122. button->border_color = nk_rgb(65,65,65);
  15123. button->text_background = nk_rgb(40,40,40);
  15124. button->text_normal = nk_rgb(175,175,175);
  15125. button->text_hover = nk_rgb(175,175,175);
  15126. button->text_active = nk_rgb(175,175,175);
  15127. button->padding = nk_vec2(4.0f,4.0f);
  15128. button->touch_padding = nk_vec2(0.0f,0.0f);
  15129. button->userdata = nk_handle_ptr(0);
  15130. button->text_alignment = NK_TEXT_CENTERED;
  15131. button->border = 1.0f;
  15132. button->rounding = 0.0f;
  15133. button->draw_begin = 0;
  15134. button->draw_end = 0;
  15135. style->scrollh.dec_button = style->scrollh.inc_button;
  15136. style->scrollv.inc_button = style->scrollh.inc_button;
  15137. style->scrollv.dec_button = style->scrollh.inc_button;
  15138. /* edit */
  15139. edit = &style->edit;
  15140. nk_zero_struct(*edit);
  15141. edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
  15142. edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
  15143. edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
  15144. edit->cursor_normal = table[NK_COLOR_TEXT];
  15145. edit->cursor_hover = table[NK_COLOR_TEXT];
  15146. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  15147. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  15148. edit->border_color = table[NK_COLOR_BORDER];
  15149. edit->text_normal = table[NK_COLOR_TEXT];
  15150. edit->text_hover = table[NK_COLOR_TEXT];
  15151. edit->text_active = table[NK_COLOR_TEXT];
  15152. edit->selected_normal = table[NK_COLOR_TEXT];
  15153. edit->selected_hover = table[NK_COLOR_TEXT];
  15154. edit->selected_text_normal = table[NK_COLOR_EDIT];
  15155. edit->selected_text_hover = table[NK_COLOR_EDIT];
  15156. edit->scrollbar_size = nk_vec2(10,10);
  15157. edit->scrollbar = style->scrollv;
  15158. edit->padding = nk_vec2(4,4);
  15159. edit->row_padding = 2;
  15160. edit->cursor_size = 4;
  15161. edit->border = 1;
  15162. edit->rounding = 0;
  15163. /* property */
  15164. property = &style->property;
  15165. nk_zero_struct(*property);
  15166. property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15167. property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15168. property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15169. property->border_color = table[NK_COLOR_BORDER];
  15170. property->label_normal = table[NK_COLOR_TEXT];
  15171. property->label_hover = table[NK_COLOR_TEXT];
  15172. property->label_active = table[NK_COLOR_TEXT];
  15173. property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
  15174. property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
  15175. property->userdata = nk_handle_ptr(0);
  15176. property->padding = nk_vec2(4,4);
  15177. property->border = 1;
  15178. property->rounding = 10;
  15179. property->draw_begin = 0;
  15180. property->draw_end = 0;
  15181. /* property buttons */
  15182. button = &style->property.dec_button;
  15183. nk_zero_struct(*button);
  15184. button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15185. button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15186. button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15187. button->border_color = nk_rgba(0,0,0,0);
  15188. button->text_background = table[NK_COLOR_PROPERTY];
  15189. button->text_normal = table[NK_COLOR_TEXT];
  15190. button->text_hover = table[NK_COLOR_TEXT];
  15191. button->text_active = table[NK_COLOR_TEXT];
  15192. button->padding = nk_vec2(0.0f,0.0f);
  15193. button->touch_padding = nk_vec2(0.0f,0.0f);
  15194. button->userdata = nk_handle_ptr(0);
  15195. button->text_alignment = NK_TEXT_CENTERED;
  15196. button->border = 0.0f;
  15197. button->rounding = 0.0f;
  15198. button->draw_begin = 0;
  15199. button->draw_end = 0;
  15200. style->property.inc_button = style->property.dec_button;
  15201. /* property edit */
  15202. edit = &style->property.edit;
  15203. nk_zero_struct(*edit);
  15204. edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15205. edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15206. edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  15207. edit->border_color = nk_rgba(0,0,0,0);
  15208. edit->cursor_normal = table[NK_COLOR_TEXT];
  15209. edit->cursor_hover = table[NK_COLOR_TEXT];
  15210. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  15211. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  15212. edit->text_normal = table[NK_COLOR_TEXT];
  15213. edit->text_hover = table[NK_COLOR_TEXT];
  15214. edit->text_active = table[NK_COLOR_TEXT];
  15215. edit->selected_normal = table[NK_COLOR_TEXT];
  15216. edit->selected_hover = table[NK_COLOR_TEXT];
  15217. edit->selected_text_normal = table[NK_COLOR_EDIT];
  15218. edit->selected_text_hover = table[NK_COLOR_EDIT];
  15219. edit->padding = nk_vec2(0,0);
  15220. edit->cursor_size = 8;
  15221. edit->border = 0;
  15222. edit->rounding = 0;
  15223. /* chart */
  15224. chart = &style->chart;
  15225. nk_zero_struct(*chart);
  15226. chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
  15227. chart->border_color = table[NK_COLOR_BORDER];
  15228. chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
  15229. chart->color = table[NK_COLOR_CHART_COLOR];
  15230. chart->padding = nk_vec2(4,4);
  15231. chart->border = 0;
  15232. chart->rounding = 0;
  15233. /* combo */
  15234. combo = &style->combo;
  15235. combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  15236. combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  15237. combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  15238. combo->border_color = table[NK_COLOR_BORDER];
  15239. combo->label_normal = table[NK_COLOR_TEXT];
  15240. combo->label_hover = table[NK_COLOR_TEXT];
  15241. combo->label_active = table[NK_COLOR_TEXT];
  15242. combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
  15243. combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
  15244. combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
  15245. combo->content_padding = nk_vec2(4,4);
  15246. combo->button_padding = nk_vec2(0,4);
  15247. combo->spacing = nk_vec2(4,0);
  15248. combo->border = 1;
  15249. combo->rounding = 0;
  15250. /* combo button */
  15251. button = &style->combo.button;
  15252. nk_zero_struct(*button);
  15253. button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  15254. button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  15255. button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  15256. button->border_color = nk_rgba(0,0,0,0);
  15257. button->text_background = table[NK_COLOR_COMBO];
  15258. button->text_normal = table[NK_COLOR_TEXT];
  15259. button->text_hover = table[NK_COLOR_TEXT];
  15260. button->text_active = table[NK_COLOR_TEXT];
  15261. button->padding = nk_vec2(2.0f,2.0f);
  15262. button->touch_padding = nk_vec2(0.0f,0.0f);
  15263. button->userdata = nk_handle_ptr(0);
  15264. button->text_alignment = NK_TEXT_CENTERED;
  15265. button->border = 0.0f;
  15266. button->rounding = 0.0f;
  15267. button->draw_begin = 0;
  15268. button->draw_end = 0;
  15269. /* tab */
  15270. tab = &style->tab;
  15271. tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  15272. tab->border_color = table[NK_COLOR_BORDER];
  15273. tab->text = table[NK_COLOR_TEXT];
  15274. tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
  15275. tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
  15276. tab->padding = nk_vec2(4,4);
  15277. tab->spacing = nk_vec2(4,4);
  15278. tab->indent = 10.0f;
  15279. tab->border = 1;
  15280. tab->rounding = 0;
  15281. /* tab button */
  15282. button = &style->tab.tab_minimize_button;
  15283. nk_zero_struct(*button);
  15284. button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  15285. button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  15286. button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  15287. button->border_color = nk_rgba(0,0,0,0);
  15288. button->text_background = table[NK_COLOR_TAB_HEADER];
  15289. button->text_normal = table[NK_COLOR_TEXT];
  15290. button->text_hover = table[NK_COLOR_TEXT];
  15291. button->text_active = table[NK_COLOR_TEXT];
  15292. button->padding = nk_vec2(2.0f,2.0f);
  15293. button->touch_padding = nk_vec2(0.0f,0.0f);
  15294. button->userdata = nk_handle_ptr(0);
  15295. button->text_alignment = NK_TEXT_CENTERED;
  15296. button->border = 0.0f;
  15297. button->rounding = 0.0f;
  15298. button->draw_begin = 0;
  15299. button->draw_end = 0;
  15300. style->tab.tab_maximize_button =*button;
  15301. /* node button */
  15302. button = &style->tab.node_minimize_button;
  15303. nk_zero_struct(*button);
  15304. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  15305. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  15306. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  15307. button->border_color = nk_rgba(0,0,0,0);
  15308. button->text_background = table[NK_COLOR_TAB_HEADER];
  15309. button->text_normal = table[NK_COLOR_TEXT];
  15310. button->text_hover = table[NK_COLOR_TEXT];
  15311. button->text_active = table[NK_COLOR_TEXT];
  15312. button->padding = nk_vec2(2.0f,2.0f);
  15313. button->touch_padding = nk_vec2(0.0f,0.0f);
  15314. button->userdata = nk_handle_ptr(0);
  15315. button->text_alignment = NK_TEXT_CENTERED;
  15316. button->border = 0.0f;
  15317. button->rounding = 0.0f;
  15318. button->draw_begin = 0;
  15319. button->draw_end = 0;
  15320. style->tab.node_maximize_button =*button;
  15321. /* window header */
  15322. win = &style->window;
  15323. win->header.align = NK_HEADER_RIGHT;
  15324. win->header.close_symbol = NK_SYMBOL_X;
  15325. win->header.minimize_symbol = NK_SYMBOL_MINUS;
  15326. win->header.maximize_symbol = NK_SYMBOL_PLUS;
  15327. win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  15328. win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  15329. win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
  15330. win->header.label_normal = table[NK_COLOR_TEXT];
  15331. win->header.label_hover = table[NK_COLOR_TEXT];
  15332. win->header.label_active = table[NK_COLOR_TEXT];
  15333. win->header.label_padding = nk_vec2(4,4);
  15334. win->header.padding = nk_vec2(4,4);
  15335. win->header.spacing = nk_vec2(0,0);
  15336. /* window header close button */
  15337. button = &style->window.header.close_button;
  15338. nk_zero_struct(*button);
  15339. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  15340. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  15341. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  15342. button->border_color = nk_rgba(0,0,0,0);
  15343. button->text_background = table[NK_COLOR_HEADER];
  15344. button->text_normal = table[NK_COLOR_TEXT];
  15345. button->text_hover = table[NK_COLOR_TEXT];
  15346. button->text_active = table[NK_COLOR_TEXT];
  15347. button->padding = nk_vec2(0.0f,0.0f);
  15348. button->touch_padding = nk_vec2(0.0f,0.0f);
  15349. button->userdata = nk_handle_ptr(0);
  15350. button->text_alignment = NK_TEXT_CENTERED;
  15351. button->border = 0.0f;
  15352. button->rounding = 0.0f;
  15353. button->draw_begin = 0;
  15354. button->draw_end = 0;
  15355. /* window header minimize button */
  15356. button = &style->window.header.minimize_button;
  15357. nk_zero_struct(*button);
  15358. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  15359. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  15360. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  15361. button->border_color = nk_rgba(0,0,0,0);
  15362. button->text_background = table[NK_COLOR_HEADER];
  15363. button->text_normal = table[NK_COLOR_TEXT];
  15364. button->text_hover = table[NK_COLOR_TEXT];
  15365. button->text_active = table[NK_COLOR_TEXT];
  15366. button->padding = nk_vec2(0.0f,0.0f);
  15367. button->touch_padding = nk_vec2(0.0f,0.0f);
  15368. button->userdata = nk_handle_ptr(0);
  15369. button->text_alignment = NK_TEXT_CENTERED;
  15370. button->border = 0.0f;
  15371. button->rounding = 0.0f;
  15372. button->draw_begin = 0;
  15373. button->draw_end = 0;
  15374. /* window */
  15375. win->background = table[NK_COLOR_WINDOW];
  15376. win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
  15377. win->border_color = table[NK_COLOR_BORDER];
  15378. win->popup_border_color = table[NK_COLOR_BORDER];
  15379. win->combo_border_color = table[NK_COLOR_BORDER];
  15380. win->contextual_border_color = table[NK_COLOR_BORDER];
  15381. win->menu_border_color = table[NK_COLOR_BORDER];
  15382. win->group_border_color = table[NK_COLOR_BORDER];
  15383. win->tooltip_border_color = table[NK_COLOR_BORDER];
  15384. win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
  15385. win->rounding = 0.0f;
  15386. win->spacing = nk_vec2(4,4);
  15387. win->scrollbar_size = nk_vec2(10,10);
  15388. win->min_size = nk_vec2(64,64);
  15389. win->combo_border = 1.0f;
  15390. win->contextual_border = 1.0f;
  15391. win->menu_border = 1.0f;
  15392. win->group_border = 1.0f;
  15393. win->tooltip_border = 1.0f;
  15394. win->popup_border = 1.0f;
  15395. win->border = 2.0f;
  15396. win->min_row_height_padding = 8;
  15397. win->padding = nk_vec2(4,4);
  15398. win->group_padding = nk_vec2(4,4);
  15399. win->popup_padding = nk_vec2(4,4);
  15400. win->combo_padding = nk_vec2(4,4);
  15401. win->contextual_padding = nk_vec2(4,4);
  15402. win->menu_padding = nk_vec2(4,4);
  15403. win->tooltip_padding = nk_vec2(4,4);
  15404. }
  15405. NK_API void
  15406. nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
  15407. {
  15408. struct nk_style *style;
  15409. NK_ASSERT(ctx);
  15410. if (!ctx) return;
  15411. style = &ctx->style;
  15412. style->font = font;
  15413. ctx->stacks.fonts.head = 0;
  15414. if (ctx->current)
  15415. nk_layout_reset_min_row_height(ctx);
  15416. }
  15417. NK_API int
  15418. nk_style_push_font(struct nk_context *ctx, const struct nk_user_font *font)
  15419. {
  15420. struct nk_config_stack_user_font *font_stack;
  15421. struct nk_config_stack_user_font_element *element;
  15422. NK_ASSERT(ctx);
  15423. if (!ctx) return 0;
  15424. font_stack = &ctx->stacks.fonts;
  15425. NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
  15426. if (font_stack->head >= (int)NK_LEN(font_stack->elements))
  15427. return 0;
  15428. element = &font_stack->elements[font_stack->head++];
  15429. element->address = &ctx->style.font;
  15430. element->old_value = ctx->style.font;
  15431. ctx->style.font = font;
  15432. return 1;
  15433. }
  15434. NK_API int
  15435. nk_style_pop_font(struct nk_context *ctx)
  15436. {
  15437. struct nk_config_stack_user_font *font_stack;
  15438. struct nk_config_stack_user_font_element *element;
  15439. NK_ASSERT(ctx);
  15440. if (!ctx) return 0;
  15441. font_stack = &ctx->stacks.fonts;
  15442. NK_ASSERT(font_stack->head > 0);
  15443. if (font_stack->head < 1)
  15444. return 0;
  15445. element = &font_stack->elements[--font_stack->head];
  15446. *element->address = element->old_value;
  15447. return 1;
  15448. }
  15449. #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
  15450. nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
  15451. {\
  15452. struct nk_config_stack_##type * type_stack;\
  15453. struct nk_config_stack_##type##_element *element;\
  15454. NK_ASSERT(ctx);\
  15455. if (!ctx) return 0;\
  15456. type_stack = &ctx->stacks.stack;\
  15457. NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
  15458. if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
  15459. return 0;\
  15460. element = &type_stack->elements[type_stack->head++];\
  15461. element->address = address;\
  15462. element->old_value = *address;\
  15463. *address = value;\
  15464. return 1;\
  15465. }
  15466. #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
  15467. nk_style_pop_##type(struct nk_context *ctx)\
  15468. {\
  15469. struct nk_config_stack_##type *type_stack;\
  15470. struct nk_config_stack_##type##_element *element;\
  15471. NK_ASSERT(ctx);\
  15472. if (!ctx) return 0;\
  15473. type_stack = &ctx->stacks.stack;\
  15474. NK_ASSERT(type_stack->head > 0);\
  15475. if (type_stack->head < 1)\
  15476. return 0;\
  15477. element = &type_stack->elements[--type_stack->head];\
  15478. *element->address = element->old_value;\
  15479. return 1;\
  15480. }
  15481. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
  15482. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,float, floats)
  15483. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
  15484. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,flags, flags)
  15485. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk,color, colors)
  15486. NK_API int NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
  15487. NK_API int NK_STYLE_POP_IMPLEMENATION(float,floats)
  15488. NK_API int NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
  15489. NK_API int NK_STYLE_POP_IMPLEMENATION(flags,flags)
  15490. NK_API int NK_STYLE_POP_IMPLEMENATION(color,colors)
  15491. NK_API int
  15492. nk_style_set_cursor(struct nk_context *ctx, enum nk_style_cursor c)
  15493. {
  15494. struct nk_style *style;
  15495. NK_ASSERT(ctx);
  15496. if (!ctx) return 0;
  15497. style = &ctx->style;
  15498. if (style->cursors[c]) {
  15499. style->cursor_active = style->cursors[c];
  15500. return 1;
  15501. }
  15502. return 0;
  15503. }
  15504. NK_API void
  15505. nk_style_show_cursor(struct nk_context *ctx)
  15506. {
  15507. ctx->style.cursor_visible = nk_true;
  15508. }
  15509. NK_API void
  15510. nk_style_hide_cursor(struct nk_context *ctx)
  15511. {
  15512. ctx->style.cursor_visible = nk_false;
  15513. }
  15514. NK_API void
  15515. nk_style_load_cursor(struct nk_context *ctx, enum nk_style_cursor cursor,
  15516. const struct nk_cursor *c)
  15517. {
  15518. struct nk_style *style;
  15519. NK_ASSERT(ctx);
  15520. if (!ctx) return;
  15521. style = &ctx->style;
  15522. style->cursors[cursor] = c;
  15523. }
  15524. NK_API void
  15525. nk_style_load_all_cursors(struct nk_context *ctx, struct nk_cursor *cursors)
  15526. {
  15527. int i = 0;
  15528. struct nk_style *style;
  15529. NK_ASSERT(ctx);
  15530. if (!ctx) return;
  15531. style = &ctx->style;
  15532. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  15533. style->cursors[i] = &cursors[i];
  15534. style->cursor_visible = nk_true;
  15535. }
  15536. /* ===============================================================
  15537. *
  15538. * POOL
  15539. *
  15540. * ===============================================================*/
  15541. NK_INTERN void
  15542. nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc,
  15543. unsigned int capacity)
  15544. {
  15545. nk_zero(pool, sizeof(*pool));
  15546. pool->alloc = *alloc;
  15547. pool->capacity = capacity;
  15548. pool->type = NK_BUFFER_DYNAMIC;
  15549. pool->pages = 0;
  15550. }
  15551. NK_INTERN void
  15552. nk_pool_free(struct nk_pool *pool)
  15553. {
  15554. struct nk_page *iter = pool->pages;
  15555. if (!pool) return;
  15556. if (pool->type == NK_BUFFER_FIXED) return;
  15557. while (iter) {
  15558. struct nk_page *next = iter->next;
  15559. pool->alloc.free(pool->alloc.userdata, iter);
  15560. iter = next;
  15561. }
  15562. }
  15563. NK_INTERN void
  15564. nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
  15565. {
  15566. nk_zero(pool, sizeof(*pool));
  15567. NK_ASSERT(size >= sizeof(struct nk_page));
  15568. if (size < sizeof(struct nk_page)) return;
  15569. pool->capacity = (unsigned)(size - sizeof(struct nk_page)) / sizeof(struct nk_page_element);
  15570. pool->pages = (struct nk_page*)memory;
  15571. pool->type = NK_BUFFER_FIXED;
  15572. pool->size = size;
  15573. }
  15574. NK_INTERN struct nk_page_element*
  15575. nk_pool_alloc(struct nk_pool *pool)
  15576. {
  15577. if (!pool->pages || pool->pages->size >= pool->capacity) {
  15578. /* allocate new page */
  15579. struct nk_page *page;
  15580. if (pool->type == NK_BUFFER_FIXED) {
  15581. if (!pool->pages) {
  15582. NK_ASSERT(pool->pages);
  15583. return 0;
  15584. }
  15585. NK_ASSERT(pool->pages->size < pool->capacity);
  15586. return 0;
  15587. } else {
  15588. nk_size size = sizeof(struct nk_page);
  15589. size += NK_POOL_DEFAULT_CAPACITY * sizeof(union nk_page_data);
  15590. page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
  15591. page->next = pool->pages;
  15592. pool->pages = page;
  15593. page->size = 0;
  15594. }
  15595. }
  15596. return &pool->pages->win[pool->pages->size++];
  15597. }
  15598. /* ===============================================================
  15599. *
  15600. * CONTEXT
  15601. *
  15602. * ===============================================================*/
  15603. NK_INTERN void* nk_create_window(struct nk_context *ctx);
  15604. NK_INTERN void nk_remove_window(struct nk_context*, struct nk_window*);
  15605. NK_INTERN void nk_free_window(struct nk_context *ctx, struct nk_window *win);
  15606. NK_INTERN void nk_free_table(struct nk_context *ctx, struct nk_table *tbl);
  15607. NK_INTERN void nk_remove_table(struct nk_window *win, struct nk_table *tbl);
  15608. NK_INTERN void* nk_create_panel(struct nk_context *ctx);
  15609. NK_INTERN void nk_free_panel(struct nk_context*, struct nk_panel *pan);
  15610. NK_INTERN void
  15611. nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
  15612. {
  15613. NK_ASSERT(ctx);
  15614. if (!ctx) return;
  15615. nk_zero_struct(*ctx);
  15616. nk_style_default(ctx);
  15617. ctx->seq = 1;
  15618. if (font) ctx->style.font = font;
  15619. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  15620. nk_draw_list_init(&ctx->draw_list);
  15621. #endif
  15622. }
  15623. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  15624. NK_API int
  15625. nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
  15626. {
  15627. struct nk_allocator alloc;
  15628. alloc.userdata.ptr = 0;
  15629. alloc.alloc = nk_malloc;
  15630. alloc.free = nk_mfree;
  15631. return nk_init(ctx, &alloc, font);
  15632. }
  15633. #endif
  15634. NK_API int
  15635. nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
  15636. const struct nk_user_font *font)
  15637. {
  15638. NK_ASSERT(memory);
  15639. if (!memory) return 0;
  15640. nk_setup(ctx, font);
  15641. nk_buffer_init_fixed(&ctx->memory, memory, size);
  15642. ctx->use_pool = nk_false;
  15643. return 1;
  15644. }
  15645. NK_API int
  15646. nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
  15647. struct nk_buffer *pool, const struct nk_user_font *font)
  15648. {
  15649. NK_ASSERT(cmds);
  15650. NK_ASSERT(pool);
  15651. if (!cmds || !pool) return 0;
  15652. nk_setup(ctx, font);
  15653. ctx->memory = *cmds;
  15654. if (pool->type == NK_BUFFER_FIXED) {
  15655. /* take memory from buffer and alloc fixed pool */
  15656. nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
  15657. } else {
  15658. /* create dynamic pool from buffer allocator */
  15659. struct nk_allocator *alloc = &pool->pool;
  15660. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  15661. }
  15662. ctx->use_pool = nk_true;
  15663. return 1;
  15664. }
  15665. NK_API int
  15666. nk_init(struct nk_context *ctx, struct nk_allocator *alloc,
  15667. const struct nk_user_font *font)
  15668. {
  15669. NK_ASSERT(alloc);
  15670. if (!alloc) return 0;
  15671. nk_setup(ctx, font);
  15672. nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
  15673. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  15674. ctx->use_pool = nk_true;
  15675. return 1;
  15676. }
  15677. #ifdef NK_INCLUDE_COMMAND_USERDATA
  15678. NK_API void
  15679. nk_set_user_data(struct nk_context *ctx, nk_handle handle)
  15680. {
  15681. if (!ctx) return;
  15682. ctx->userdata = handle;
  15683. if (ctx->current)
  15684. ctx->current->buffer.userdata = handle;
  15685. }
  15686. #endif
  15687. NK_API void
  15688. nk_free(struct nk_context *ctx)
  15689. {
  15690. NK_ASSERT(ctx);
  15691. if (!ctx) return;
  15692. nk_buffer_free(&ctx->memory);
  15693. if (ctx->use_pool)
  15694. nk_pool_free(&ctx->pool);
  15695. nk_zero(&ctx->input, sizeof(ctx->input));
  15696. nk_zero(&ctx->style, sizeof(ctx->style));
  15697. nk_zero(&ctx->memory, sizeof(ctx->memory));
  15698. ctx->seq = 0;
  15699. ctx->build = 0;
  15700. ctx->begin = 0;
  15701. ctx->end = 0;
  15702. ctx->active = 0;
  15703. ctx->current = 0;
  15704. ctx->freelist = 0;
  15705. ctx->count = 0;
  15706. }
  15707. NK_API void
  15708. nk_clear(struct nk_context *ctx)
  15709. {
  15710. struct nk_window *iter;
  15711. struct nk_window *next;
  15712. NK_ASSERT(ctx);
  15713. if (!ctx) return;
  15714. if (ctx->use_pool)
  15715. nk_buffer_clear(&ctx->memory);
  15716. else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
  15717. ctx->build = 0;
  15718. ctx->memory.calls = 0;
  15719. ctx->last_widget_state = 0;
  15720. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  15721. NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
  15722. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  15723. nk_draw_list_clear(&ctx->draw_list);
  15724. #endif
  15725. /* garbage collector */
  15726. iter = ctx->begin;
  15727. while (iter) {
  15728. /* make sure minimized windows do not get removed */
  15729. if ((iter->flags & NK_WINDOW_MINIMIZED) &&
  15730. !(iter->flags & NK_WINDOW_CLOSED)) {
  15731. iter = iter->next;
  15732. continue;
  15733. }
  15734. /* remove hotness from hidden or closed windows*/
  15735. if (((iter->flags & NK_WINDOW_HIDDEN) ||
  15736. (iter->flags & NK_WINDOW_CLOSED)) &&
  15737. iter == ctx->active)
  15738. ctx->active = iter->next;
  15739. /* free unused popup windows */
  15740. if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
  15741. nk_free_window(ctx, iter->popup.win);
  15742. iter->popup.win = 0;
  15743. }
  15744. /* remove unused window state tables */
  15745. {struct nk_table *n, *it = iter->tables;
  15746. while (it) {
  15747. n = it->next;
  15748. if (it->seq != ctx->seq) {
  15749. nk_remove_table(iter, it);
  15750. nk_zero(it, sizeof(union nk_page_data));
  15751. nk_free_table(ctx, it);
  15752. if (it == iter->tables)
  15753. iter->tables = n;
  15754. }
  15755. it = n;
  15756. }}
  15757. /* window itself is not used anymore so free */
  15758. if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
  15759. next = iter->next;
  15760. nk_remove_window(ctx, iter);
  15761. nk_free_window(ctx, iter);
  15762. iter = next;
  15763. } else iter = iter->next;
  15764. }
  15765. ctx->seq++;
  15766. }
  15767. /* ----------------------------------------------------------------
  15768. *
  15769. * BUFFERING
  15770. *
  15771. * ---------------------------------------------------------------*/
  15772. NK_INTERN void
  15773. nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  15774. {
  15775. NK_ASSERT(ctx);
  15776. NK_ASSERT(buffer);
  15777. if (!ctx || !buffer) return;
  15778. buffer->begin = ctx->memory.allocated;
  15779. buffer->end = buffer->begin;
  15780. buffer->last = buffer->begin;
  15781. buffer->clip = nk_null_rect;
  15782. }
  15783. NK_INTERN void
  15784. nk_start(struct nk_context *ctx, struct nk_window *win)
  15785. {
  15786. NK_ASSERT(ctx);
  15787. NK_ASSERT(win);
  15788. nk_start_buffer(ctx, &win->buffer);
  15789. }
  15790. NK_INTERN void
  15791. nk_start_popup(struct nk_context *ctx, struct nk_window *win)
  15792. {
  15793. struct nk_popup_buffer *buf;
  15794. NK_ASSERT(ctx);
  15795. NK_ASSERT(win);
  15796. if (!ctx || !win) return;
  15797. /* save buffer fill state for popup */
  15798. buf = &win->popup.buf;
  15799. buf->begin = win->buffer.end;
  15800. buf->end = win->buffer.end;
  15801. buf->parent = win->buffer.last;
  15802. buf->last = buf->begin;
  15803. buf->active = nk_true;
  15804. }
  15805. NK_INTERN void
  15806. nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
  15807. {
  15808. struct nk_popup_buffer *buf;
  15809. NK_ASSERT(ctx);
  15810. NK_ASSERT(win);
  15811. if (!ctx || !win) return;
  15812. buf = &win->popup.buf;
  15813. buf->last = win->buffer.last;
  15814. buf->end = win->buffer.end;
  15815. }
  15816. NK_INTERN void
  15817. nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  15818. {
  15819. NK_ASSERT(ctx);
  15820. NK_ASSERT(buffer);
  15821. if (!ctx || !buffer) return;
  15822. buffer->end = ctx->memory.allocated;
  15823. }
  15824. NK_INTERN void
  15825. nk_finish(struct nk_context *ctx, struct nk_window *win)
  15826. {
  15827. struct nk_popup_buffer *buf;
  15828. struct nk_command *parent_last;
  15829. void *memory;
  15830. NK_ASSERT(ctx);
  15831. NK_ASSERT(win);
  15832. if (!ctx || !win) return;
  15833. nk_finish_buffer(ctx, &win->buffer);
  15834. if (!win->popup.buf.active) return;
  15835. buf = &win->popup.buf;
  15836. memory = ctx->memory.memory.ptr;
  15837. parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
  15838. parent_last->next = buf->end;
  15839. }
  15840. NK_INTERN void
  15841. nk_build(struct nk_context *ctx)
  15842. {
  15843. struct nk_window *iter = 0;
  15844. struct nk_command *cmd = 0;
  15845. nk_byte *buffer = 0;
  15846. /* draw cursor overlay */
  15847. if (!ctx->style.cursor_active)
  15848. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  15849. if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
  15850. struct nk_rect mouse_bounds;
  15851. const struct nk_cursor *cursor = ctx->style.cursor_active;
  15852. nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
  15853. nk_start_buffer(ctx, &ctx->overlay);
  15854. mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
  15855. mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
  15856. mouse_bounds.w = cursor->size.x;
  15857. mouse_bounds.h = cursor->size.y;
  15858. nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
  15859. nk_finish_buffer(ctx, &ctx->overlay);
  15860. }
  15861. /* build one big draw command list out of all window buffers */
  15862. iter = ctx->begin;
  15863. buffer = (nk_byte*)ctx->memory.memory.ptr;
  15864. while (iter != 0) {
  15865. struct nk_window *next = iter->next;
  15866. if (iter->buffer.last == iter->buffer.begin || (iter->flags & NK_WINDOW_HIDDEN)||
  15867. iter->seq != ctx->seq)
  15868. goto cont;
  15869. cmd = nk_ptr_add(struct nk_command, buffer, iter->buffer.last);
  15870. while (next && ((next->buffer.last == next->buffer.begin) ||
  15871. (next->flags & NK_WINDOW_HIDDEN)))
  15872. next = next->next; /* skip empty command buffers */
  15873. if (next) cmd->next = next->buffer.begin;
  15874. cont: iter = next;
  15875. }
  15876. /* append all popup draw commands into lists */
  15877. iter = ctx->begin;
  15878. while (iter != 0) {
  15879. struct nk_window *next = iter->next;
  15880. struct nk_popup_buffer *buf;
  15881. if (!iter->popup.buf.active)
  15882. goto skip;
  15883. buf = &iter->popup.buf;
  15884. cmd->next = buf->begin;
  15885. cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
  15886. buf->active = nk_false;
  15887. skip: iter = next;
  15888. }
  15889. /* append overlay commands */
  15890. if (cmd) {
  15891. if (ctx->overlay.end != ctx->overlay.begin)
  15892. cmd->next = ctx->overlay.begin;
  15893. else cmd->next = ctx->memory.allocated;
  15894. }
  15895. }
  15896. NK_API const struct nk_command*
  15897. nk__begin(struct nk_context *ctx)
  15898. {
  15899. struct nk_window *iter;
  15900. nk_byte *buffer;
  15901. NK_ASSERT(ctx);
  15902. if (!ctx) return 0;
  15903. if (!ctx->count) return 0;
  15904. buffer = (nk_byte*)ctx->memory.memory.ptr;
  15905. if (!ctx->build) {
  15906. nk_build(ctx);
  15907. ctx->build = nk_true;
  15908. }
  15909. iter = ctx->begin;
  15910. while (iter && ((iter->buffer.begin == iter->buffer.end) || (iter->flags & NK_WINDOW_HIDDEN)))
  15911. iter = iter->next;
  15912. if (!iter) return 0;
  15913. return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
  15914. }
  15915. NK_API const struct nk_command*
  15916. nk__next(struct nk_context *ctx, const struct nk_command *cmd)
  15917. {
  15918. nk_byte *buffer;
  15919. const struct nk_command *next;
  15920. NK_ASSERT(ctx);
  15921. if (!ctx || !cmd || !ctx->count) return 0;
  15922. if (cmd->next >= ctx->memory.allocated) return 0;
  15923. buffer = (nk_byte*)ctx->memory.memory.ptr;
  15924. next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
  15925. return next;
  15926. }
  15927. /* ----------------------------------------------------------------
  15928. *
  15929. * PANEL
  15930. *
  15931. * ---------------------------------------------------------------*/
  15932. static int
  15933. nk_panel_has_header(nk_flags flags, const char *title)
  15934. {
  15935. int active = 0;
  15936. active = (flags & (NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE));
  15937. active = active || (flags & NK_WINDOW_TITLE);
  15938. active = active && !(flags & NK_WINDOW_HIDDEN) && title;
  15939. return active;
  15940. }
  15941. NK_INTERN struct nk_vec2
  15942. nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
  15943. {
  15944. switch (type) {
  15945. default:
  15946. case NK_PANEL_WINDOW: return style->window.padding;
  15947. case NK_PANEL_GROUP: return style->window.group_padding;
  15948. case NK_PANEL_POPUP: return style->window.popup_padding;
  15949. case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
  15950. case NK_PANEL_COMBO: return style->window.combo_padding;
  15951. case NK_PANEL_MENU: return style->window.menu_padding;
  15952. case NK_PANEL_TOOLTIP: return style->window.menu_padding;
  15953. }
  15954. }
  15955. NK_INTERN float
  15956. nk_panel_get_border(const struct nk_style *style, nk_flags flags,
  15957. enum nk_panel_type type)
  15958. {
  15959. if (flags & NK_WINDOW_BORDER) {
  15960. switch (type) {
  15961. default:
  15962. case NK_PANEL_WINDOW: return style->window.border;
  15963. case NK_PANEL_GROUP: return style->window.group_border;
  15964. case NK_PANEL_POPUP: return style->window.popup_border;
  15965. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
  15966. case NK_PANEL_COMBO: return style->window.combo_border;
  15967. case NK_PANEL_MENU: return style->window.menu_border;
  15968. case NK_PANEL_TOOLTIP: return style->window.menu_border;
  15969. }} else return 0;
  15970. }
  15971. NK_INTERN struct nk_color
  15972. nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
  15973. {
  15974. switch (type) {
  15975. default:
  15976. case NK_PANEL_WINDOW: return style->window.border_color;
  15977. case NK_PANEL_GROUP: return style->window.group_border_color;
  15978. case NK_PANEL_POPUP: return style->window.popup_border_color;
  15979. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
  15980. case NK_PANEL_COMBO: return style->window.combo_border_color;
  15981. case NK_PANEL_MENU: return style->window.menu_border_color;
  15982. case NK_PANEL_TOOLTIP: return style->window.menu_border_color;
  15983. }
  15984. }
  15985. NK_INTERN int
  15986. nk_panel_is_sub(enum nk_panel_type type)
  15987. {
  15988. return (type & NK_PANEL_SET_SUB)?1:0;
  15989. }
  15990. NK_INTERN int
  15991. nk_panel_is_nonblock(enum nk_panel_type type)
  15992. {
  15993. return (type & NK_PANEL_SET_NONBLOCK)?1:0;
  15994. }
  15995. NK_INTERN int
  15996. nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type)
  15997. {
  15998. struct nk_input *in;
  15999. struct nk_window *win;
  16000. struct nk_panel *layout;
  16001. struct nk_command_buffer *out;
  16002. const struct nk_style *style;
  16003. const struct nk_user_font *font;
  16004. struct nk_vec2 scrollbar_size;
  16005. struct nk_vec2 panel_padding;
  16006. NK_ASSERT(ctx);
  16007. NK_ASSERT(ctx->current);
  16008. NK_ASSERT(ctx->current->layout);
  16009. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  16010. nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
  16011. if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
  16012. nk_zero(ctx->current->layout, sizeof(struct nk_panel));
  16013. ctx->current->layout->type = panel_type;
  16014. return 0;
  16015. }
  16016. /* pull state into local stack */
  16017. style = &ctx->style;
  16018. font = style->font;
  16019. win = ctx->current;
  16020. layout = win->layout;
  16021. out = &win->buffer;
  16022. in = (win->flags & NK_WINDOW_NO_INPUT) ? 0: &ctx->input;
  16023. #ifdef NK_INCLUDE_COMMAND_USERDATA
  16024. win->buffer.userdata = ctx->userdata;
  16025. #endif
  16026. /* pull style configuration into local stack */
  16027. scrollbar_size = style->window.scrollbar_size;
  16028. panel_padding = nk_panel_get_padding(style, panel_type);
  16029. /* window movement */
  16030. if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
  16031. int left_mouse_down;
  16032. int left_mouse_click_in_cursor;
  16033. /* calculate draggable window space */
  16034. struct nk_rect header;
  16035. header.x = win->bounds.x;
  16036. header.y = win->bounds.y;
  16037. header.w = win->bounds.w;
  16038. if (nk_panel_has_header(win->flags, title)) {
  16039. header.h = font->height + 2.0f * style->window.header.padding.y;
  16040. header.h += 2.0f * style->window.header.label_padding.y;
  16041. } else header.h = panel_padding.y;
  16042. /* window movement by dragging */
  16043. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  16044. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  16045. NK_BUTTON_LEFT, header, nk_true);
  16046. if (left_mouse_down && left_mouse_click_in_cursor) {
  16047. win->bounds.x = win->bounds.x + in->mouse.delta.x;
  16048. win->bounds.y = win->bounds.y + in->mouse.delta.y;
  16049. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
  16050. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
  16051. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
  16052. }
  16053. }
  16054. /* setup panel */
  16055. layout->type = panel_type;
  16056. layout->flags = win->flags;
  16057. layout->bounds = win->bounds;
  16058. layout->bounds.x += panel_padding.x;
  16059. layout->bounds.w -= 2*panel_padding.x;
  16060. if (win->flags & NK_WINDOW_BORDER) {
  16061. layout->border = nk_panel_get_border(style, win->flags, panel_type);
  16062. layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
  16063. } else layout->border = 0;
  16064. layout->at_y = layout->bounds.y;
  16065. layout->at_x = layout->bounds.x;
  16066. layout->max_x = 0;
  16067. layout->header_height = 0;
  16068. layout->footer_height = 0;
  16069. nk_layout_reset_min_row_height(ctx);
  16070. layout->row.index = 0;
  16071. layout->row.columns = 0;
  16072. layout->row.ratio = 0;
  16073. layout->row.item_width = 0;
  16074. layout->row.tree_depth = 0;
  16075. layout->row.height = panel_padding.y;
  16076. layout->has_scrolling = nk_true;
  16077. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
  16078. layout->bounds.w -= scrollbar_size.x;
  16079. if (!nk_panel_is_nonblock(panel_type)) {
  16080. layout->footer_height = 0;
  16081. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
  16082. layout->footer_height = scrollbar_size.y;
  16083. layout->bounds.h -= layout->footer_height;
  16084. }
  16085. /* panel header */
  16086. if (nk_panel_has_header(win->flags, title))
  16087. {
  16088. struct nk_text text;
  16089. struct nk_rect header;
  16090. const struct nk_style_item *background = 0;
  16091. /* calculate header bounds */
  16092. header.x = win->bounds.x;
  16093. header.y = win->bounds.y;
  16094. header.w = win->bounds.w;
  16095. header.h = font->height + 2.0f * style->window.header.padding.y;
  16096. header.h += (2.0f * style->window.header.label_padding.y);
  16097. /* shrink panel by header */
  16098. layout->header_height = header.h;
  16099. layout->bounds.y += header.h;
  16100. layout->bounds.h -= header.h;
  16101. layout->at_y += header.h;
  16102. /* select correct header background and text color */
  16103. if (ctx->active == win) {
  16104. background = &style->window.header.active;
  16105. text.text = style->window.header.label_active;
  16106. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
  16107. background = &style->window.header.hover;
  16108. text.text = style->window.header.label_hover;
  16109. } else {
  16110. background = &style->window.header.normal;
  16111. text.text = style->window.header.label_normal;
  16112. }
  16113. /* draw header background */
  16114. header.h += 1.0f;
  16115. if (background->type == NK_STYLE_ITEM_IMAGE) {
  16116. text.background = nk_rgba(0,0,0,0);
  16117. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  16118. } else {
  16119. text.background = background->data.color;
  16120. nk_fill_rect(out, header, 0, background->data.color);
  16121. }
  16122. /* window close button */
  16123. {struct nk_rect button;
  16124. button.y = header.y + style->window.header.padding.y;
  16125. button.h = header.h - 2 * style->window.header.padding.y;
  16126. button.w = button.h;
  16127. if (win->flags & NK_WINDOW_CLOSABLE) {
  16128. nk_flags ws = 0;
  16129. if (style->window.header.align == NK_HEADER_RIGHT) {
  16130. button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
  16131. header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
  16132. } else {
  16133. button.x = header.x + style->window.header.padding.x;
  16134. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  16135. }
  16136. if (nk_do_button_symbol(&ws, &win->buffer, button,
  16137. style->window.header.close_symbol, NK_BUTTON_DEFAULT,
  16138. &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  16139. {
  16140. layout->flags |= NK_WINDOW_HIDDEN;
  16141. layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
  16142. }
  16143. }
  16144. /* window minimize button */
  16145. if (win->flags & NK_WINDOW_MINIMIZABLE) {
  16146. nk_flags ws = 0;
  16147. if (style->window.header.align == NK_HEADER_RIGHT) {
  16148. button.x = (header.w + header.x) - button.w;
  16149. if (!(win->flags & NK_WINDOW_CLOSABLE)) {
  16150. button.x -= style->window.header.padding.x;
  16151. header.w -= style->window.header.padding.x;
  16152. }
  16153. header.w -= button.w + style->window.header.spacing.x;
  16154. } else {
  16155. button.x = header.x;
  16156. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  16157. }
  16158. if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED)?
  16159. style->window.header.maximize_symbol: style->window.header.minimize_symbol,
  16160. NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  16161. layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
  16162. layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED:
  16163. layout->flags | NK_WINDOW_MINIMIZED;
  16164. }}
  16165. {/* window header title */
  16166. int text_len = nk_strlen(title);
  16167. struct nk_rect label = {0,0,0,0};
  16168. float t = font->width(font->userdata, font->height, title, text_len);
  16169. text.padding = nk_vec2(0,0);
  16170. label.x = header.x + style->window.header.padding.x;
  16171. label.x += style->window.header.label_padding.x;
  16172. label.y = header.y + style->window.header.label_padding.y;
  16173. label.h = font->height + 2 * style->window.header.label_padding.y;
  16174. label.w = t + 2 * style->window.header.spacing.x;
  16175. label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
  16176. nk_widget_text(out, label,(const char*)title, text_len, &text, NK_TEXT_LEFT, font);}
  16177. }
  16178. /* draw window background */
  16179. if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
  16180. struct nk_rect body;
  16181. body.x = win->bounds.x;
  16182. body.w = win->bounds.w;
  16183. body.y = (win->bounds.y + layout->header_height);
  16184. body.h = (win->bounds.h - layout->header_height);
  16185. if (style->window.fixed_background.type == NK_STYLE_ITEM_IMAGE)
  16186. nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
  16187. else nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
  16188. }
  16189. /* set clipping rectangle */
  16190. {struct nk_rect clip;
  16191. layout->clip = layout->bounds;
  16192. nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
  16193. layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
  16194. nk_push_scissor(out, clip);
  16195. layout->clip = clip;}
  16196. return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
  16197. }
  16198. NK_INTERN void
  16199. nk_panel_end(struct nk_context *ctx)
  16200. {
  16201. struct nk_input *in;
  16202. struct nk_window *window;
  16203. struct nk_panel *layout;
  16204. const struct nk_style *style;
  16205. struct nk_command_buffer *out;
  16206. struct nk_vec2 scrollbar_size;
  16207. struct nk_vec2 panel_padding;
  16208. NK_ASSERT(ctx);
  16209. NK_ASSERT(ctx->current);
  16210. NK_ASSERT(ctx->current->layout);
  16211. if (!ctx || !ctx->current || !ctx->current->layout)
  16212. return;
  16213. window = ctx->current;
  16214. layout = window->layout;
  16215. style = &ctx->style;
  16216. out = &window->buffer;
  16217. in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 :&ctx->input;
  16218. if (!nk_panel_is_sub(layout->type))
  16219. nk_push_scissor(out, nk_null_rect);
  16220. /* cache configuration data */
  16221. scrollbar_size = style->window.scrollbar_size;
  16222. panel_padding = nk_panel_get_padding(style, layout->type);
  16223. /* update the current cursor Y-position to point over the last added widget */
  16224. layout->at_y += layout->row.height;
  16225. /* dynamic panels */
  16226. if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
  16227. {
  16228. /* update panel height to fit dynamic growth */
  16229. struct nk_rect empty_space;
  16230. if (layout->at_y < (layout->bounds.y + layout->bounds.h))
  16231. layout->bounds.h = layout->at_y - layout->bounds.y;
  16232. /* fill top empty space */
  16233. empty_space.x = window->bounds.x;
  16234. empty_space.y = layout->bounds.y;
  16235. empty_space.h = panel_padding.y;
  16236. empty_space.w = window->bounds.w;
  16237. nk_fill_rect(out, empty_space, 0, style->window.background);
  16238. /* fill left empty space */
  16239. empty_space.x = window->bounds.x;
  16240. empty_space.y = layout->bounds.y;
  16241. empty_space.w = panel_padding.x + layout->border;
  16242. empty_space.h = layout->bounds.h;
  16243. nk_fill_rect(out, empty_space, 0, style->window.background);
  16244. /* fill right empty space */
  16245. empty_space.x = layout->bounds.x + layout->bounds.w - layout->border;
  16246. empty_space.y = layout->bounds.y;
  16247. empty_space.w = panel_padding.x + layout->border;
  16248. empty_space.h = layout->bounds.h;
  16249. if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
  16250. empty_space.w += scrollbar_size.x;
  16251. nk_fill_rect(out, empty_space, 0, style->window.background);
  16252. /* fill bottom empty space */
  16253. if (*layout->offset_x != 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR)) {
  16254. empty_space.x = window->bounds.x;
  16255. empty_space.y = layout->bounds.y + layout->bounds.h;
  16256. empty_space.w = window->bounds.w;
  16257. empty_space.h = scrollbar_size.y;
  16258. nk_fill_rect(out, empty_space, 0, style->window.background);
  16259. }
  16260. }
  16261. /* scrollbars */
  16262. if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
  16263. !(layout->flags & NK_WINDOW_MINIMIZED) &&
  16264. window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
  16265. {
  16266. struct nk_rect scroll;
  16267. int scroll_has_scrolling;
  16268. float scroll_target;
  16269. float scroll_offset;
  16270. float scroll_step;
  16271. float scroll_inc;
  16272. /* mouse wheel scrolling */
  16273. if (nk_panel_is_sub(layout->type))
  16274. {
  16275. /* sub-window mouse wheel scrolling */
  16276. struct nk_window *root_window = window;
  16277. struct nk_panel *root_panel = window->layout;
  16278. while (root_panel->parent)
  16279. root_panel = root_panel->parent;
  16280. while (root_window->parent)
  16281. root_window = root_window->parent;
  16282. /* only allow scrolling if parent window is active */
  16283. scroll_has_scrolling = 0;
  16284. if ((root_window == ctx->active) && layout->has_scrolling) {
  16285. /* and panel is being hovered and inside clip rect*/
  16286. if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
  16287. NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
  16288. root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
  16289. {
  16290. /* deactivate all parent scrolling */
  16291. root_panel = window->layout;
  16292. while (root_panel->parent) {
  16293. root_panel->has_scrolling = nk_false;
  16294. root_panel = root_panel->parent;
  16295. }
  16296. root_panel->has_scrolling = nk_false;
  16297. scroll_has_scrolling = nk_true;
  16298. }
  16299. }
  16300. } else if (!nk_panel_is_sub(layout->type)) {
  16301. /* window mouse wheel scrolling */
  16302. scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
  16303. if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
  16304. window->scrolled = nk_true;
  16305. else window->scrolled = nk_false;
  16306. } else scroll_has_scrolling = nk_false;
  16307. {
  16308. /* vertical scrollbar */
  16309. nk_flags state = 0;
  16310. scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  16311. scroll.y = layout->bounds.y;
  16312. scroll.w = scrollbar_size.x;
  16313. scroll.h = layout->bounds.h;
  16314. scroll_offset = (float)*layout->offset_y;
  16315. scroll_step = scroll.h * 0.10f;
  16316. scroll_inc = scroll.h * 0.01f;
  16317. scroll_target = (float)(int)(layout->at_y - scroll.y);
  16318. scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
  16319. scroll_offset, scroll_target, scroll_step, scroll_inc,
  16320. &ctx->style.scrollv, in, style->font);
  16321. *layout->offset_y = (nk_uint)scroll_offset;
  16322. if (in && scroll_has_scrolling)
  16323. in->mouse.scroll_delta.y = 0;
  16324. }
  16325. {
  16326. /* horizontal scrollbar */
  16327. nk_flags state = 0;
  16328. scroll.x = layout->bounds.x;
  16329. scroll.y = layout->bounds.y + layout->bounds.h;
  16330. scroll.w = layout->bounds.w;
  16331. scroll.h = scrollbar_size.y;
  16332. scroll_offset = (float)*layout->offset_x;
  16333. scroll_target = (float)(int)(layout->max_x - scroll.x);
  16334. scroll_step = layout->max_x * 0.05f;
  16335. scroll_inc = layout->max_x * 0.005f;
  16336. scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
  16337. scroll_offset, scroll_target, scroll_step, scroll_inc,
  16338. &ctx->style.scrollh, in, style->font);
  16339. *layout->offset_x = (nk_uint)scroll_offset;
  16340. }
  16341. }
  16342. /* hide scroll if no user input */
  16343. if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
  16344. int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
  16345. int is_window_hovered = nk_window_is_hovered(ctx);
  16346. int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  16347. if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
  16348. window->scrollbar_hiding_timer += ctx->delta_time_seconds;
  16349. else window->scrollbar_hiding_timer = 0;
  16350. } else window->scrollbar_hiding_timer = 0;
  16351. /* window border */
  16352. if (layout->flags & NK_WINDOW_BORDER)
  16353. {
  16354. struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
  16355. const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED) ?
  16356. style->window.border + window->bounds.y + layout->header_height:
  16357. (layout->flags & NK_WINDOW_DYNAMIC)?
  16358. layout->bounds.y + layout->bounds.h + layout->footer_height:
  16359. window->bounds.y + window->bounds.h;
  16360. /* draw border top */
  16361. nk_stroke_line(out,window->bounds.x,window->bounds.y,
  16362. window->bounds.x + window->bounds.w, window->bounds.y,
  16363. layout->border, border_color);
  16364. /* draw bottom border */
  16365. nk_stroke_line(out, window->bounds.x, padding_y,
  16366. window->bounds.x + window->bounds.w, padding_y, layout->border, border_color);
  16367. /* draw left border */
  16368. nk_stroke_line(out, window->bounds.x + layout->border*0.5f,
  16369. window->bounds.y, window->bounds.x + layout->border*0.5f,
  16370. padding_y, layout->border, border_color);
  16371. /* draw right border */
  16372. nk_stroke_line(out, window->bounds.x + window->bounds.w - layout->border*0.5f,
  16373. window->bounds.y, window->bounds.x + window->bounds.w - layout->border*0.5f,
  16374. padding_y, layout->border, border_color);
  16375. }
  16376. /* scaler */
  16377. if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
  16378. {
  16379. /* calculate scaler bounds */
  16380. struct nk_rect scaler;
  16381. scaler.w = scrollbar_size.x;
  16382. scaler.h = scrollbar_size.y;
  16383. scaler.y = layout->bounds.y + layout->bounds.h;
  16384. if (layout->flags & NK_WINDOW_SCALE_LEFT)
  16385. scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
  16386. else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  16387. if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
  16388. scaler.x -= scaler.w;
  16389. /* draw scaler */
  16390. {const struct nk_style_item *item = &style->window.scaler;
  16391. if (item->type == NK_STYLE_ITEM_IMAGE)
  16392. nk_draw_image(out, scaler, &item->data.image, nk_white);
  16393. else {
  16394. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  16395. nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
  16396. scaler.y + scaler.h, scaler.x + scaler.w,
  16397. scaler.y + scaler.h, item->data.color);
  16398. } else {
  16399. nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
  16400. scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
  16401. }
  16402. }}
  16403. /* do window scaling */
  16404. if (!(window->flags & NK_WINDOW_ROM)) {
  16405. struct nk_vec2 window_size = style->window.min_size;
  16406. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  16407. int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
  16408. NK_BUTTON_LEFT, scaler, nk_true);
  16409. if (left_mouse_down && left_mouse_click_in_scaler) {
  16410. float delta_x = in->mouse.delta.x;
  16411. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  16412. delta_x = -delta_x;
  16413. window->bounds.x += in->mouse.delta.x;
  16414. }
  16415. /* dragging in x-direction */
  16416. if (window->bounds.w + delta_x >= window_size.x) {
  16417. if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
  16418. window->bounds.w = window->bounds.w + delta_x;
  16419. scaler.x += in->mouse.delta.x;
  16420. }
  16421. }
  16422. /* dragging in y-direction (only possible if static window) */
  16423. if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
  16424. if (window_size.y < window->bounds.h + in->mouse.delta.y) {
  16425. if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
  16426. window->bounds.h = window->bounds.h + in->mouse.delta.y;
  16427. scaler.y += in->mouse.delta.y;
  16428. }
  16429. }
  16430. }
  16431. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
  16432. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w/2.0f;
  16433. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h/2.0f;
  16434. }
  16435. }
  16436. }
  16437. if (!nk_panel_is_sub(layout->type)) {
  16438. /* window is hidden so clear command buffer */
  16439. if (layout->flags & NK_WINDOW_HIDDEN)
  16440. nk_command_buffer_reset(&window->buffer);
  16441. /* window is visible and not tab */
  16442. else nk_finish(ctx, window);
  16443. }
  16444. /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
  16445. if (layout->flags & NK_WINDOW_REMOVE_ROM) {
  16446. layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  16447. layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  16448. }
  16449. window->flags = layout->flags;
  16450. /* property garbage collector */
  16451. if (window->property.active && window->property.old != window->property.seq &&
  16452. window->property.active == window->property.prev) {
  16453. nk_zero(&window->property, sizeof(window->property));
  16454. } else {
  16455. window->property.old = window->property.seq;
  16456. window->property.prev = window->property.active;
  16457. window->property.seq = 0;
  16458. }
  16459. /* edit garbage collector */
  16460. if (window->edit.active && window->edit.old != window->edit.seq &&
  16461. window->edit.active == window->edit.prev) {
  16462. nk_zero(&window->edit, sizeof(window->edit));
  16463. } else {
  16464. window->edit.old = window->edit.seq;
  16465. window->edit.prev = window->edit.active;
  16466. window->edit.seq = 0;
  16467. }
  16468. /* contextual garbage collector */
  16469. if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
  16470. window->popup.con_count = 0;
  16471. window->popup.con_old = 0;
  16472. window->popup.active_con = 0;
  16473. } else {
  16474. window->popup.con_old = window->popup.con_count;
  16475. window->popup.con_count = 0;
  16476. }
  16477. window->popup.combo_count = 0;
  16478. /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
  16479. NK_ASSERT(!layout->row.tree_depth);
  16480. }
  16481. /* ----------------------------------------------------------------
  16482. *
  16483. * PAGE ELEMENT
  16484. *
  16485. * ---------------------------------------------------------------*/
  16486. NK_INTERN struct nk_page_element*
  16487. nk_create_page_element(struct nk_context *ctx)
  16488. {
  16489. struct nk_page_element *elem;
  16490. if (ctx->freelist) {
  16491. /* unlink page element from free list */
  16492. elem = ctx->freelist;
  16493. ctx->freelist = elem->next;
  16494. } else if (ctx->use_pool) {
  16495. /* allocate page element from memory pool */
  16496. elem = nk_pool_alloc(&ctx->pool);
  16497. NK_ASSERT(elem);
  16498. if (!elem) return 0;
  16499. } else {
  16500. /* allocate new page element from back of fixed size memory buffer */
  16501. NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
  16502. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
  16503. elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
  16504. NK_ASSERT(elem);
  16505. if (!elem) return 0;
  16506. }
  16507. nk_zero_struct(*elem);
  16508. elem->next = 0;
  16509. elem->prev = 0;
  16510. return elem;
  16511. }
  16512. NK_INTERN void
  16513. nk_link_page_element_into_freelist(struct nk_context *ctx,
  16514. struct nk_page_element *elem)
  16515. {
  16516. /* link table into freelist */
  16517. if (!ctx->freelist) {
  16518. ctx->freelist = elem;
  16519. } else {
  16520. elem->next = ctx->freelist;
  16521. ctx->freelist = elem;
  16522. }
  16523. }
  16524. NK_INTERN void
  16525. nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
  16526. {
  16527. /* we have a pool so just add to free list */
  16528. if (ctx->use_pool) {
  16529. nk_link_page_element_into_freelist(ctx, elem);
  16530. return;
  16531. }
  16532. /* if possible remove last element from back of fixed memory buffer */
  16533. {void *elem_end = (void*)(elem + 1);
  16534. void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
  16535. if (elem_end == buffer_end)
  16536. ctx->memory.size -= sizeof(struct nk_page_element);
  16537. else nk_link_page_element_into_freelist(ctx, elem);}
  16538. }
  16539. /* ----------------------------------------------------------------
  16540. *
  16541. * PANEL
  16542. *
  16543. * ---------------------------------------------------------------*/
  16544. NK_INTERN void*
  16545. nk_create_panel(struct nk_context *ctx)
  16546. {
  16547. struct nk_page_element *elem;
  16548. elem = nk_create_page_element(ctx);
  16549. if (!elem) return 0;
  16550. nk_zero_struct(*elem);
  16551. return &elem->data.pan;
  16552. }
  16553. NK_INTERN void
  16554. nk_free_panel(struct nk_context *ctx, struct nk_panel *pan)
  16555. {
  16556. union nk_page_data *pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
  16557. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  16558. nk_free_page_element(ctx, pe);
  16559. }
  16560. /* ----------------------------------------------------------------
  16561. *
  16562. * TABLES
  16563. *
  16564. * ---------------------------------------------------------------*/
  16565. NK_INTERN struct nk_table*
  16566. nk_create_table(struct nk_context *ctx)
  16567. {
  16568. struct nk_page_element *elem;
  16569. elem = nk_create_page_element(ctx);
  16570. if (!elem) return 0;
  16571. nk_zero_struct(*elem);
  16572. return &elem->data.tbl;
  16573. }
  16574. NK_INTERN void
  16575. nk_free_table(struct nk_context *ctx, struct nk_table *tbl)
  16576. {
  16577. union nk_page_data *pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
  16578. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  16579. nk_free_page_element(ctx, pe);
  16580. }
  16581. NK_INTERN void
  16582. nk_push_table(struct nk_window *win, struct nk_table *tbl)
  16583. {
  16584. if (!win->tables) {
  16585. win->tables = tbl;
  16586. tbl->next = 0;
  16587. tbl->prev = 0;
  16588. tbl->size = 0;
  16589. win->table_count = 1;
  16590. return;
  16591. }
  16592. win->tables->prev = tbl;
  16593. tbl->next = win->tables;
  16594. tbl->prev = 0;
  16595. tbl->size = 0;
  16596. win->tables = tbl;
  16597. win->table_count++;
  16598. }
  16599. NK_INTERN void
  16600. nk_remove_table(struct nk_window *win, struct nk_table *tbl)
  16601. {
  16602. if (win->tables == tbl)
  16603. win->tables = tbl->next;
  16604. if (tbl->next)
  16605. tbl->next->prev = tbl->prev;
  16606. if (tbl->prev)
  16607. tbl->prev->next = tbl->next;
  16608. tbl->next = 0;
  16609. tbl->prev = 0;
  16610. }
  16611. NK_INTERN nk_uint*
  16612. nk_add_value(struct nk_context *ctx, struct nk_window *win,
  16613. nk_hash name, nk_uint value)
  16614. {
  16615. NK_ASSERT(ctx);
  16616. NK_ASSERT(win);
  16617. if (!win || !ctx) return 0;
  16618. if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
  16619. struct nk_table *tbl = nk_create_table(ctx);
  16620. NK_ASSERT(tbl);
  16621. if (!tbl) return 0;
  16622. nk_push_table(win, tbl);
  16623. }
  16624. win->tables->seq = win->seq;
  16625. win->tables->keys[win->tables->size] = name;
  16626. win->tables->values[win->tables->size] = value;
  16627. return &win->tables->values[win->tables->size++];
  16628. }
  16629. NK_INTERN nk_uint*
  16630. nk_find_value(struct nk_window *win, nk_hash name)
  16631. {
  16632. struct nk_table *iter = win->tables;
  16633. while (iter) {
  16634. unsigned int i = 0;
  16635. unsigned int size = iter->size;
  16636. for (i = 0; i < size; ++i) {
  16637. if (iter->keys[i] == name) {
  16638. iter->seq = win->seq;
  16639. return &iter->values[i];
  16640. }
  16641. } size = NK_VALUE_PAGE_CAPACITY;
  16642. iter = iter->next;
  16643. }
  16644. return 0;
  16645. }
  16646. /* ----------------------------------------------------------------
  16647. *
  16648. * WINDOW
  16649. *
  16650. * ---------------------------------------------------------------*/
  16651. NK_INTERN void*
  16652. nk_create_window(struct nk_context *ctx)
  16653. {
  16654. struct nk_page_element *elem;
  16655. elem = nk_create_page_element(ctx);
  16656. if (!elem) return 0;
  16657. elem->data.win.seq = ctx->seq;
  16658. return &elem->data.win;
  16659. }
  16660. NK_INTERN void
  16661. nk_free_window(struct nk_context *ctx, struct nk_window *win)
  16662. {
  16663. /* unlink windows from list */
  16664. struct nk_table *it = win->tables;
  16665. if (win->popup.win) {
  16666. nk_free_window(ctx, win->popup.win);
  16667. win->popup.win = 0;
  16668. }
  16669. win->next = 0;
  16670. win->prev = 0;
  16671. while (it) {
  16672. /*free window state tables */
  16673. struct nk_table *n = it->next;
  16674. nk_remove_table(win, it);
  16675. nk_free_table(ctx, it);
  16676. if (it == win->tables)
  16677. win->tables = n;
  16678. it = n;
  16679. }
  16680. /* link windows into freelist */
  16681. {union nk_page_data *pd = NK_CONTAINER_OF(win, union nk_page_data, win);
  16682. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  16683. nk_free_page_element(ctx, pe);}
  16684. }
  16685. NK_INTERN struct nk_window*
  16686. nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name)
  16687. {
  16688. struct nk_window *iter;
  16689. iter = ctx->begin;
  16690. while (iter) {
  16691. NK_ASSERT(iter != iter->next);
  16692. if (iter->name == hash) {
  16693. int max_len = nk_strlen(iter->name_string);
  16694. if (!nk_stricmpn(iter->name_string, name, max_len))
  16695. return iter;
  16696. }
  16697. iter = iter->next;
  16698. }
  16699. return 0;
  16700. }
  16701. enum nk_window_insert_location {
  16702. NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
  16703. NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
  16704. };
  16705. NK_INTERN void
  16706. nk_insert_window(struct nk_context *ctx, struct nk_window *win,
  16707. enum nk_window_insert_location loc)
  16708. {
  16709. const struct nk_window *iter;
  16710. NK_ASSERT(ctx);
  16711. NK_ASSERT(win);
  16712. if (!win || !ctx) return;
  16713. iter = ctx->begin;
  16714. while (iter) {
  16715. NK_ASSERT(iter != iter->next);
  16716. NK_ASSERT(iter != win);
  16717. if (iter == win) return;
  16718. iter = iter->next;
  16719. }
  16720. if (!ctx->begin) {
  16721. win->next = 0;
  16722. win->prev = 0;
  16723. ctx->begin = win;
  16724. ctx->end = win;
  16725. ctx->count = 1;
  16726. return;
  16727. }
  16728. if (loc == NK_INSERT_BACK) {
  16729. struct nk_window *end;
  16730. end = ctx->end;
  16731. end->flags |= NK_WINDOW_ROM;
  16732. end->next = win;
  16733. win->prev = ctx->end;
  16734. win->next = 0;
  16735. ctx->end = win;
  16736. ctx->active = ctx->end;
  16737. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  16738. } else {
  16739. ctx->end->flags |= NK_WINDOW_ROM;
  16740. ctx->begin->prev = win;
  16741. win->next = ctx->begin;
  16742. win->prev = 0;
  16743. ctx->begin = win;
  16744. ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
  16745. }
  16746. ctx->count++;
  16747. }
  16748. NK_INTERN void
  16749. nk_remove_window(struct nk_context *ctx, struct nk_window *win)
  16750. {
  16751. if (win == ctx->begin || win == ctx->end) {
  16752. if (win == ctx->begin) {
  16753. ctx->begin = win->next;
  16754. if (win->next)
  16755. win->next->prev = 0;
  16756. }
  16757. if (win == ctx->end) {
  16758. ctx->end = win->prev;
  16759. if (win->prev)
  16760. win->prev->next = 0;
  16761. }
  16762. } else {
  16763. if (win->next)
  16764. win->next->prev = win->prev;
  16765. if (win->prev)
  16766. win->prev->next = win->next;
  16767. }
  16768. if (win == ctx->active || !ctx->active) {
  16769. ctx->active = ctx->end;
  16770. if (ctx->end)
  16771. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  16772. }
  16773. win->next = 0;
  16774. win->prev = 0;
  16775. ctx->count--;
  16776. }
  16777. NK_API int
  16778. nk_begin(struct nk_context *ctx, const char *title,
  16779. struct nk_rect bounds, nk_flags flags)
  16780. {
  16781. return nk_begin_titled(ctx, title, title, bounds, flags);
  16782. }
  16783. NK_API int
  16784. nk_begin_titled(struct nk_context *ctx, const char *name, const char *title,
  16785. struct nk_rect bounds, nk_flags flags)
  16786. {
  16787. struct nk_window *win;
  16788. struct nk_style *style;
  16789. nk_hash title_hash;
  16790. int title_len;
  16791. int ret = 0;
  16792. NK_ASSERT(ctx);
  16793. NK_ASSERT(name);
  16794. NK_ASSERT(title);
  16795. NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
  16796. NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
  16797. if (!ctx || ctx->current || !title || !name)
  16798. return 0;
  16799. /* find or create window */
  16800. style = &ctx->style;
  16801. title_len = (int)nk_strlen(name);
  16802. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  16803. win = nk_find_window(ctx, title_hash, name);
  16804. if (!win) {
  16805. /* create new window */
  16806. nk_size name_length = (nk_size)nk_strlen(name);
  16807. win = (struct nk_window*)nk_create_window(ctx);
  16808. NK_ASSERT(win);
  16809. if (!win) return 0;
  16810. if (flags & NK_WINDOW_BACKGROUND)
  16811. nk_insert_window(ctx, win, NK_INSERT_FRONT);
  16812. else nk_insert_window(ctx, win, NK_INSERT_BACK);
  16813. nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
  16814. win->flags = flags;
  16815. win->bounds = bounds;
  16816. win->name = title_hash;
  16817. name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME-1);
  16818. NK_MEMCPY(win->name_string, name, name_length);
  16819. win->name_string[name_length] = 0;
  16820. win->popup.win = 0;
  16821. if (!ctx->active)
  16822. ctx->active = win;
  16823. } else {
  16824. /* update window */
  16825. win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE-1);
  16826. win->flags |= flags;
  16827. if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
  16828. win->bounds = bounds;
  16829. /* If this assert triggers you either:
  16830. *
  16831. * I.) Have more than one window with the same name or
  16832. * II.) You forgot to actually draw the window.
  16833. * More specific you did not call `nk_clear` (nk_clear will be
  16834. * automatically called for you if you are using one of the
  16835. * provided demo backends). */
  16836. NK_ASSERT(win->seq != ctx->seq);
  16837. win->seq = ctx->seq;
  16838. if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN))
  16839. ctx->active = win;
  16840. }
  16841. if (win->flags & NK_WINDOW_HIDDEN) {
  16842. ctx->current = win;
  16843. win->layout = 0;
  16844. return 0;
  16845. }
  16846. /* window overlapping */
  16847. if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
  16848. {
  16849. int inpanel, ishovered;
  16850. const struct nk_window *iter = win;
  16851. float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
  16852. (2.0f * style->window.header.label_padding.y);
  16853. struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED))?
  16854. win->bounds: nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
  16855. /* activate window if hovered and no other window is overlapping this window */
  16856. nk_start(ctx, win);
  16857. inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
  16858. inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
  16859. ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
  16860. if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
  16861. iter = win->next;
  16862. while (iter) {
  16863. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  16864. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  16865. if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  16866. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  16867. (!(iter->flags & NK_WINDOW_HIDDEN) || !(iter->flags & NK_WINDOW_BACKGROUND)))
  16868. break;
  16869. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  16870. NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  16871. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  16872. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  16873. break;
  16874. iter = iter->next;
  16875. }
  16876. }
  16877. /* activate window if clicked */
  16878. if (iter && inpanel && (win != ctx->end) && !(iter->flags & NK_WINDOW_BACKGROUND)) {
  16879. iter = win->next;
  16880. while (iter) {
  16881. /* try to find a panel with higher priority in the same position */
  16882. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  16883. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  16884. if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
  16885. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  16886. !(iter->flags & NK_WINDOW_HIDDEN))
  16887. break;
  16888. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  16889. NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  16890. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  16891. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  16892. break;
  16893. iter = iter->next;
  16894. }
  16895. }
  16896. if (!iter && ctx->end != win) {
  16897. if (!(win->flags & NK_WINDOW_BACKGROUND)) {
  16898. /* current window is active in that position so transfer to top
  16899. * at the highest priority in stack */
  16900. nk_remove_window(ctx, win);
  16901. nk_insert_window(ctx, win, NK_INSERT_BACK);
  16902. }
  16903. win->flags &= ~(nk_flags)NK_WINDOW_ROM;
  16904. ctx->active = win;
  16905. }
  16906. if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
  16907. win->flags |= NK_WINDOW_ROM;
  16908. }
  16909. win->layout = (struct nk_panel*)nk_create_panel(ctx);
  16910. ctx->current = win;
  16911. ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
  16912. win->layout->offset_x = &win->scrollbar.x;
  16913. win->layout->offset_y = &win->scrollbar.y;
  16914. return ret;
  16915. }
  16916. NK_API void
  16917. nk_end(struct nk_context *ctx)
  16918. {
  16919. struct nk_panel *layout;
  16920. NK_ASSERT(ctx);
  16921. NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
  16922. if (!ctx || !ctx->current)
  16923. return;
  16924. layout = ctx->current->layout;
  16925. if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
  16926. ctx->current = 0;
  16927. return;
  16928. }
  16929. nk_panel_end(ctx);
  16930. nk_free_panel(ctx, ctx->current->layout);
  16931. ctx->current = 0;
  16932. }
  16933. NK_API struct nk_rect
  16934. nk_window_get_bounds(const struct nk_context *ctx)
  16935. {
  16936. NK_ASSERT(ctx);
  16937. NK_ASSERT(ctx->current);
  16938. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  16939. return ctx->current->bounds;
  16940. }
  16941. NK_API struct nk_vec2
  16942. nk_window_get_position(const struct nk_context *ctx)
  16943. {
  16944. NK_ASSERT(ctx);
  16945. NK_ASSERT(ctx->current);
  16946. if (!ctx || !ctx->current) return nk_vec2(0,0);
  16947. return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
  16948. }
  16949. NK_API struct nk_vec2
  16950. nk_window_get_size(const struct nk_context *ctx)
  16951. {
  16952. NK_ASSERT(ctx);
  16953. NK_ASSERT(ctx->current);
  16954. if (!ctx || !ctx->current) return nk_vec2(0,0);
  16955. return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
  16956. }
  16957. NK_API float
  16958. nk_window_get_width(const struct nk_context *ctx)
  16959. {
  16960. NK_ASSERT(ctx);
  16961. NK_ASSERT(ctx->current);
  16962. if (!ctx || !ctx->current) return 0;
  16963. return ctx->current->bounds.w;
  16964. }
  16965. NK_API float
  16966. nk_window_get_height(const struct nk_context *ctx)
  16967. {
  16968. NK_ASSERT(ctx);
  16969. NK_ASSERT(ctx->current);
  16970. if (!ctx || !ctx->current) return 0;
  16971. return ctx->current->bounds.h;
  16972. }
  16973. NK_API struct nk_rect
  16974. nk_window_get_content_region(struct nk_context *ctx)
  16975. {
  16976. NK_ASSERT(ctx);
  16977. NK_ASSERT(ctx->current);
  16978. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  16979. return ctx->current->layout->clip;
  16980. }
  16981. NK_API struct nk_vec2
  16982. nk_window_get_content_region_min(struct nk_context *ctx)
  16983. {
  16984. NK_ASSERT(ctx);
  16985. NK_ASSERT(ctx->current);
  16986. NK_ASSERT(ctx->current->layout);
  16987. if (!ctx || !ctx->current) return nk_vec2(0,0);
  16988. return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
  16989. }
  16990. NK_API struct nk_vec2
  16991. nk_window_get_content_region_max(struct nk_context *ctx)
  16992. {
  16993. NK_ASSERT(ctx);
  16994. NK_ASSERT(ctx->current);
  16995. NK_ASSERT(ctx->current->layout);
  16996. if (!ctx || !ctx->current) return nk_vec2(0,0);
  16997. return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
  16998. ctx->current->layout->clip.y + ctx->current->layout->clip.h);
  16999. }
  17000. NK_API struct nk_vec2
  17001. nk_window_get_content_region_size(struct nk_context *ctx)
  17002. {
  17003. NK_ASSERT(ctx);
  17004. NK_ASSERT(ctx->current);
  17005. NK_ASSERT(ctx->current->layout);
  17006. if (!ctx || !ctx->current) return nk_vec2(0,0);
  17007. return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
  17008. }
  17009. NK_API struct nk_command_buffer*
  17010. nk_window_get_canvas(struct nk_context *ctx)
  17011. {
  17012. NK_ASSERT(ctx);
  17013. NK_ASSERT(ctx->current);
  17014. NK_ASSERT(ctx->current->layout);
  17015. if (!ctx || !ctx->current) return 0;
  17016. return &ctx->current->buffer;
  17017. }
  17018. NK_API struct nk_panel*
  17019. nk_window_get_panel(struct nk_context *ctx)
  17020. {
  17021. NK_ASSERT(ctx);
  17022. NK_ASSERT(ctx->current);
  17023. if (!ctx || !ctx->current) return 0;
  17024. return ctx->current->layout;
  17025. }
  17026. NK_API int
  17027. nk_window_has_focus(const struct nk_context *ctx)
  17028. {
  17029. NK_ASSERT(ctx);
  17030. NK_ASSERT(ctx->current);
  17031. NK_ASSERT(ctx->current->layout);
  17032. if (!ctx || !ctx->current) return 0;
  17033. return ctx->current == ctx->active;
  17034. }
  17035. NK_API int
  17036. nk_window_is_hovered(struct nk_context *ctx)
  17037. {
  17038. NK_ASSERT(ctx);
  17039. NK_ASSERT(ctx->current);
  17040. if (!ctx || !ctx->current) return 0;
  17041. return nk_input_is_mouse_hovering_rect(&ctx->input, ctx->current->bounds);
  17042. }
  17043. NK_API int
  17044. nk_window_is_any_hovered(struct nk_context *ctx)
  17045. {
  17046. struct nk_window *iter;
  17047. NK_ASSERT(ctx);
  17048. if (!ctx) return 0;
  17049. iter = ctx->begin;
  17050. while (iter) {
  17051. /* check if window is being hovered */
  17052. if (iter->flags & NK_WINDOW_MINIMIZED) {
  17053. struct nk_rect header = iter->bounds;
  17054. header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
  17055. if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
  17056. return 1;
  17057. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
  17058. return 1;
  17059. }
  17060. /* check if window popup is being hovered */
  17061. if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
  17062. return 1;
  17063. iter = iter->next;
  17064. }
  17065. return 0;
  17066. }
  17067. NK_API int
  17068. nk_item_is_any_active(struct nk_context *ctx)
  17069. {
  17070. int any_hovered = nk_window_is_any_hovered(ctx);
  17071. int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  17072. return any_hovered || any_active;
  17073. }
  17074. NK_API int
  17075. nk_window_is_collapsed(struct nk_context *ctx, const char *name)
  17076. {
  17077. int title_len;
  17078. nk_hash title_hash;
  17079. struct nk_window *win;
  17080. NK_ASSERT(ctx);
  17081. if (!ctx) return 0;
  17082. title_len = (int)nk_strlen(name);
  17083. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17084. win = nk_find_window(ctx, title_hash, name);
  17085. if (!win) return 0;
  17086. return win->flags & NK_WINDOW_MINIMIZED;
  17087. }
  17088. NK_API int
  17089. nk_window_is_closed(struct nk_context *ctx, const char *name)
  17090. {
  17091. int title_len;
  17092. nk_hash title_hash;
  17093. struct nk_window *win;
  17094. NK_ASSERT(ctx);
  17095. if (!ctx) return 1;
  17096. title_len = (int)nk_strlen(name);
  17097. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17098. win = nk_find_window(ctx, title_hash, name);
  17099. if (!win) return 1;
  17100. return (win->flags & NK_WINDOW_CLOSED);
  17101. }
  17102. NK_API int
  17103. nk_window_is_hidden(struct nk_context *ctx, const char *name)
  17104. {
  17105. int title_len;
  17106. nk_hash title_hash;
  17107. struct nk_window *win;
  17108. NK_ASSERT(ctx);
  17109. if (!ctx) return 1;
  17110. title_len = (int)nk_strlen(name);
  17111. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17112. win = nk_find_window(ctx, title_hash, name);
  17113. if (!win) return 1;
  17114. return (win->flags & NK_WINDOW_HIDDEN);
  17115. }
  17116. NK_API int
  17117. nk_window_is_active(struct nk_context *ctx, const char *name)
  17118. {
  17119. int title_len;
  17120. nk_hash title_hash;
  17121. struct nk_window *win;
  17122. NK_ASSERT(ctx);
  17123. if (!ctx) return 0;
  17124. title_len = (int)nk_strlen(name);
  17125. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17126. win = nk_find_window(ctx, title_hash, name);
  17127. if (!win) return 0;
  17128. return win == ctx->active;
  17129. }
  17130. NK_API struct nk_window*
  17131. nk_window_find(struct nk_context *ctx, const char *name)
  17132. {
  17133. int title_len;
  17134. nk_hash title_hash;
  17135. title_len = (int)nk_strlen(name);
  17136. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17137. return nk_find_window(ctx, title_hash, name);
  17138. }
  17139. NK_API void
  17140. nk_window_close(struct nk_context *ctx, const char *name)
  17141. {
  17142. struct nk_window *win;
  17143. NK_ASSERT(ctx);
  17144. if (!ctx) return;
  17145. win = nk_window_find(ctx, name);
  17146. if (!win) return;
  17147. NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
  17148. if (ctx->current == win) return;
  17149. win->flags |= NK_WINDOW_HIDDEN;
  17150. win->flags |= NK_WINDOW_CLOSED;
  17151. }
  17152. NK_API void
  17153. nk_window_set_bounds(struct nk_context *ctx, struct nk_rect bounds)
  17154. {
  17155. NK_ASSERT(ctx); NK_ASSERT(ctx->current);
  17156. if (!ctx || !ctx->current) return;
  17157. ctx->current->bounds = bounds;
  17158. }
  17159. NK_API void
  17160. nk_window_set_position(struct nk_context *ctx, struct nk_vec2 pos)
  17161. {
  17162. NK_ASSERT(ctx); NK_ASSERT(ctx->current);
  17163. if (!ctx || !ctx->current) return;
  17164. ctx->current->bounds.x = pos.x;
  17165. ctx->current->bounds.y = pos.y;
  17166. }
  17167. NK_API void
  17168. nk_window_set_size(struct nk_context *ctx, struct nk_vec2 size)
  17169. {
  17170. NK_ASSERT(ctx); NK_ASSERT(ctx->current);
  17171. if (!ctx || !ctx->current) return;
  17172. ctx->current->bounds.w = size.x;
  17173. ctx->current->bounds.h = size.y;
  17174. }
  17175. NK_API void
  17176. nk_window_collapse(struct nk_context *ctx, const char *name,
  17177. enum nk_collapse_states c)
  17178. {
  17179. int title_len;
  17180. nk_hash title_hash;
  17181. struct nk_window *win;
  17182. NK_ASSERT(ctx);
  17183. if (!ctx) return;
  17184. title_len = (int)nk_strlen(name);
  17185. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17186. win = nk_find_window(ctx, title_hash, name);
  17187. if (!win) return;
  17188. if (c == NK_MINIMIZED)
  17189. win->flags |= NK_WINDOW_MINIMIZED;
  17190. else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
  17191. }
  17192. NK_API void
  17193. nk_window_collapse_if(struct nk_context *ctx, const char *name,
  17194. enum nk_collapse_states c, int cond)
  17195. {
  17196. NK_ASSERT(ctx);
  17197. if (!ctx || !cond) return;
  17198. nk_window_collapse(ctx, name, c);
  17199. }
  17200. NK_API void
  17201. nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states s)
  17202. {
  17203. int title_len;
  17204. nk_hash title_hash;
  17205. struct nk_window *win;
  17206. NK_ASSERT(ctx);
  17207. if (!ctx) return;
  17208. title_len = (int)nk_strlen(name);
  17209. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17210. win = nk_find_window(ctx, title_hash, name);
  17211. if (!win) return;
  17212. if (s == NK_HIDDEN) {
  17213. win->flags |= NK_WINDOW_HIDDEN;
  17214. } else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
  17215. }
  17216. NK_API void
  17217. nk_window_show_if(struct nk_context *ctx, const char *name,
  17218. enum nk_show_states s, int cond)
  17219. {
  17220. NK_ASSERT(ctx);
  17221. if (!ctx || !cond) return;
  17222. nk_window_show(ctx, name, s);
  17223. }
  17224. NK_API void
  17225. nk_window_set_focus(struct nk_context *ctx, const char *name)
  17226. {
  17227. int title_len;
  17228. nk_hash title_hash;
  17229. struct nk_window *win;
  17230. NK_ASSERT(ctx);
  17231. if (!ctx) return;
  17232. title_len = (int)nk_strlen(name);
  17233. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  17234. win = nk_find_window(ctx, title_hash, name);
  17235. if (win && ctx->end != win) {
  17236. nk_remove_window(ctx, win);
  17237. nk_insert_window(ctx, win, NK_INSERT_BACK);
  17238. }
  17239. ctx->active = win;
  17240. }
  17241. /*----------------------------------------------------------------
  17242. *
  17243. * MENUBAR
  17244. *
  17245. * --------------------------------------------------------------*/
  17246. NK_API void
  17247. nk_menubar_begin(struct nk_context *ctx)
  17248. {
  17249. struct nk_panel *layout;
  17250. NK_ASSERT(ctx);
  17251. NK_ASSERT(ctx->current);
  17252. NK_ASSERT(ctx->current->layout);
  17253. if (!ctx || !ctx->current || !ctx->current->layout)
  17254. return;
  17255. layout = ctx->current->layout;
  17256. NK_ASSERT(layout->at_y == layout->bounds.y);
  17257. /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
  17258. If you want a menubar the first nuklear function after `nk_begin` has to be a
  17259. `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
  17260. widgets (also supports multiple rows).
  17261. Example:
  17262. if (nk_begin(...)) {
  17263. nk_menubar_begin(...);
  17264. nk_layout_xxxx(...);
  17265. nk_button(...);
  17266. nk_layout_xxxx(...);
  17267. nk_button(...);
  17268. nk_menubar_end(...);
  17269. }
  17270. nk_end(...);
  17271. */
  17272. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  17273. return;
  17274. layout->menu.x = layout->at_x;
  17275. layout->menu.y = layout->at_y + layout->row.height;
  17276. layout->menu.w = layout->bounds.w;
  17277. layout->menu.offset.x = *layout->offset_x;
  17278. layout->menu.offset.y = *layout->offset_y;
  17279. *layout->offset_y = 0;
  17280. }
  17281. NK_API void
  17282. nk_menubar_end(struct nk_context *ctx)
  17283. {
  17284. struct nk_window *win;
  17285. struct nk_panel *layout;
  17286. struct nk_command_buffer *out;
  17287. NK_ASSERT(ctx);
  17288. NK_ASSERT(ctx->current);
  17289. NK_ASSERT(ctx->current->layout);
  17290. if (!ctx || !ctx->current || !ctx->current->layout)
  17291. return;
  17292. win = ctx->current;
  17293. out = &win->buffer;
  17294. layout = win->layout;
  17295. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  17296. return;
  17297. layout->menu.h = layout->at_y - layout->menu.y;
  17298. layout->bounds.y += layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  17299. layout->bounds.h -= layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  17300. *layout->offset_x = layout->menu.offset.x;
  17301. *layout->offset_y = layout->menu.offset.y;
  17302. layout->at_y = layout->bounds.y - layout->row.height;
  17303. layout->clip.y = layout->bounds.y;
  17304. layout->clip.h = layout->bounds.h;
  17305. nk_push_scissor(out, layout->clip);
  17306. }
  17307. /* -------------------------------------------------------------
  17308. *
  17309. * LAYOUT
  17310. *
  17311. * --------------------------------------------------------------*/
  17312. NK_API void
  17313. nk_layout_set_min_row_height(struct nk_context *ctx, float height)
  17314. {
  17315. struct nk_window *win;
  17316. struct nk_panel *layout;
  17317. NK_ASSERT(ctx);
  17318. NK_ASSERT(ctx->current);
  17319. NK_ASSERT(ctx->current->layout);
  17320. if (!ctx || !ctx->current || !ctx->current->layout)
  17321. return;
  17322. win = ctx->current;
  17323. layout = win->layout;
  17324. layout->row.min_height = height;
  17325. }
  17326. NK_API void
  17327. nk_layout_reset_min_row_height(struct nk_context *ctx)
  17328. {
  17329. struct nk_window *win;
  17330. struct nk_panel *layout;
  17331. NK_ASSERT(ctx);
  17332. NK_ASSERT(ctx->current);
  17333. NK_ASSERT(ctx->current->layout);
  17334. if (!ctx || !ctx->current || !ctx->current->layout)
  17335. return;
  17336. win = ctx->current;
  17337. layout = win->layout;
  17338. layout->row.min_height = ctx->style.font->height;
  17339. layout->row.min_height += ctx->style.text.padding.y*2;
  17340. layout->row.min_height += ctx->style.window.min_row_height_padding*2;
  17341. }
  17342. NK_INTERN float
  17343. nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type,
  17344. float total_space, int columns)
  17345. {
  17346. float panel_padding;
  17347. float panel_spacing;
  17348. float panel_space;
  17349. struct nk_vec2 spacing;
  17350. struct nk_vec2 padding;
  17351. spacing = style->window.spacing;
  17352. padding = nk_panel_get_padding(style, type);
  17353. /* calculate the usable panel space */
  17354. panel_padding = 2 * padding.x;
  17355. panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
  17356. panel_space = total_space - panel_padding - panel_spacing;
  17357. return panel_space;
  17358. }
  17359. NK_INTERN void
  17360. nk_panel_layout(const struct nk_context *ctx, struct nk_window *win,
  17361. float height, int cols)
  17362. {
  17363. struct nk_panel *layout;
  17364. const struct nk_style *style;
  17365. struct nk_command_buffer *out;
  17366. struct nk_vec2 item_spacing;
  17367. struct nk_color color;
  17368. NK_ASSERT(ctx);
  17369. NK_ASSERT(ctx->current);
  17370. NK_ASSERT(ctx->current->layout);
  17371. if (!ctx || !ctx->current || !ctx->current->layout)
  17372. return;
  17373. /* prefetch some configuration data */
  17374. layout = win->layout;
  17375. style = &ctx->style;
  17376. out = &win->buffer;
  17377. color = style->window.background;
  17378. item_spacing = style->window.spacing;
  17379. /* if one of these triggers you forgot to add an `if` condition around either
  17380. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  17381. Example:
  17382. if (nk_begin(...) {...} nk_end(...); or
  17383. if (nk_group_begin(...) { nk_group_end(...);} */
  17384. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  17385. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  17386. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  17387. /* update the current row and set the current row layout */
  17388. layout->row.index = 0;
  17389. layout->at_y += layout->row.height;
  17390. layout->row.columns = cols;
  17391. if (height == 0.0f)
  17392. layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
  17393. else layout->row.height = height + item_spacing.y;
  17394. layout->row.item_offset = 0;
  17395. if (layout->flags & NK_WINDOW_DYNAMIC) {
  17396. /* draw background for dynamic panels */
  17397. struct nk_rect background;
  17398. background.x = win->bounds.x;
  17399. background.w = win->bounds.w;
  17400. background.y = layout->at_y - 1.0f;
  17401. background.h = layout->row.height + 1.0f;
  17402. nk_fill_rect(out, background, 0, color);
  17403. }
  17404. }
  17405. NK_INTERN void
  17406. nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt,
  17407. float height, int cols, int width)
  17408. {
  17409. /* update the current row and set the current row layout */
  17410. struct nk_window *win;
  17411. NK_ASSERT(ctx);
  17412. NK_ASSERT(ctx->current);
  17413. NK_ASSERT(ctx->current->layout);
  17414. if (!ctx || !ctx->current || !ctx->current->layout)
  17415. return;
  17416. win = ctx->current;
  17417. nk_panel_layout(ctx, win, height, cols);
  17418. if (fmt == NK_DYNAMIC)
  17419. win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
  17420. else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
  17421. win->layout->row.ratio = 0;
  17422. win->layout->row.filled = 0;
  17423. win->layout->row.item_offset = 0;
  17424. win->layout->row.item_width = (float)width;
  17425. }
  17426. NK_API float
  17427. nk_layout_ratio_from_pixel(struct nk_context *ctx, float pixel_width)
  17428. {
  17429. struct nk_window *win;
  17430. NK_ASSERT(ctx);
  17431. NK_ASSERT(pixel_width);
  17432. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  17433. win = ctx->current;
  17434. return NK_CLAMP(0.0f, pixel_width/win->bounds.x, 1.0f);
  17435. }
  17436. NK_API void
  17437. nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
  17438. {
  17439. nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
  17440. }
  17441. NK_API void
  17442. nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols)
  17443. {
  17444. nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
  17445. }
  17446. NK_API void
  17447. nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  17448. float row_height, int cols)
  17449. {
  17450. struct nk_window *win;
  17451. struct nk_panel *layout;
  17452. NK_ASSERT(ctx);
  17453. NK_ASSERT(ctx->current);
  17454. NK_ASSERT(ctx->current->layout);
  17455. if (!ctx || !ctx->current || !ctx->current->layout)
  17456. return;
  17457. win = ctx->current;
  17458. layout = win->layout;
  17459. nk_panel_layout(ctx, win, row_height, cols);
  17460. if (fmt == NK_DYNAMIC)
  17461. layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
  17462. else layout->row.type = NK_LAYOUT_STATIC_ROW;
  17463. layout->row.ratio = 0;
  17464. layout->row.filled = 0;
  17465. layout->row.item_width = 0;
  17466. layout->row.item_offset = 0;
  17467. layout->row.columns = cols;
  17468. }
  17469. NK_API void
  17470. nk_layout_row_push(struct nk_context *ctx, float ratio_or_width)
  17471. {
  17472. struct nk_window *win;
  17473. struct nk_panel *layout;
  17474. NK_ASSERT(ctx);
  17475. NK_ASSERT(ctx->current);
  17476. NK_ASSERT(ctx->current->layout);
  17477. if (!ctx || !ctx->current || !ctx->current->layout)
  17478. return;
  17479. win = ctx->current;
  17480. layout = win->layout;
  17481. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  17482. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  17483. return;
  17484. if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
  17485. float ratio = ratio_or_width;
  17486. if ((ratio + layout->row.filled) > 1.0f) return;
  17487. if (ratio > 0.0f)
  17488. layout->row.item_width = NK_SATURATE(ratio);
  17489. else layout->row.item_width = 1.0f - layout->row.filled;
  17490. } else layout->row.item_width = ratio_or_width;
  17491. }
  17492. NK_API void
  17493. nk_layout_row_end(struct nk_context *ctx)
  17494. {
  17495. struct nk_window *win;
  17496. struct nk_panel *layout;
  17497. NK_ASSERT(ctx);
  17498. NK_ASSERT(ctx->current);
  17499. NK_ASSERT(ctx->current->layout);
  17500. if (!ctx || !ctx->current || !ctx->current->layout)
  17501. return;
  17502. win = ctx->current;
  17503. layout = win->layout;
  17504. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  17505. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  17506. return;
  17507. layout->row.item_width = 0;
  17508. layout->row.item_offset = 0;
  17509. }
  17510. NK_API void
  17511. nk_layout_row(struct nk_context *ctx, enum nk_layout_format fmt,
  17512. float height, int cols, const float *ratio)
  17513. {
  17514. int i;
  17515. int n_undef = 0;
  17516. struct nk_window *win;
  17517. struct nk_panel *layout;
  17518. NK_ASSERT(ctx);
  17519. NK_ASSERT(ctx->current);
  17520. NK_ASSERT(ctx->current->layout);
  17521. if (!ctx || !ctx->current || !ctx->current->layout)
  17522. return;
  17523. win = ctx->current;
  17524. layout = win->layout;
  17525. nk_panel_layout(ctx, win, height, cols);
  17526. if (fmt == NK_DYNAMIC) {
  17527. /* calculate width of undefined widget ratios */
  17528. float r = 0;
  17529. layout->row.ratio = ratio;
  17530. for (i = 0; i < cols; ++i) {
  17531. if (ratio[i] < 0.0f)
  17532. n_undef++;
  17533. else r += ratio[i];
  17534. }
  17535. r = NK_SATURATE(1.0f - r);
  17536. layout->row.type = NK_LAYOUT_DYNAMIC;
  17537. layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef):0;
  17538. } else {
  17539. layout->row.ratio = ratio;
  17540. layout->row.type = NK_LAYOUT_STATIC;
  17541. layout->row.item_width = 0;
  17542. layout->row.item_offset = 0;
  17543. }
  17544. layout->row.item_offset = 0;
  17545. layout->row.filled = 0;
  17546. }
  17547. NK_API void
  17548. nk_layout_row_template_begin(struct nk_context *ctx, float height)
  17549. {
  17550. struct nk_window *win;
  17551. struct nk_panel *layout;
  17552. NK_ASSERT(ctx);
  17553. NK_ASSERT(ctx->current);
  17554. NK_ASSERT(ctx->current->layout);
  17555. if (!ctx || !ctx->current || !ctx->current->layout)
  17556. return;
  17557. win = ctx->current;
  17558. layout = win->layout;
  17559. nk_panel_layout(ctx, win, height, 1);
  17560. layout->row.type = NK_LAYOUT_TEMPLATE;
  17561. layout->row.columns = 0;
  17562. layout->row.ratio = 0;
  17563. layout->row.item_width = 0;
  17564. layout->row.item_height = 0;
  17565. layout->row.item_offset = 0;
  17566. layout->row.filled = 0;
  17567. layout->row.item.x = 0;
  17568. layout->row.item.y = 0;
  17569. layout->row.item.w = 0;
  17570. layout->row.item.h = 0;
  17571. }
  17572. NK_API void
  17573. nk_layout_row_template_push_dynamic(struct nk_context *ctx)
  17574. {
  17575. struct nk_window *win;
  17576. struct nk_panel *layout;
  17577. NK_ASSERT(ctx);
  17578. NK_ASSERT(ctx->current);
  17579. NK_ASSERT(ctx->current->layout);
  17580. if (!ctx || !ctx->current || !ctx->current->layout)
  17581. return;
  17582. win = ctx->current;
  17583. layout = win->layout;
  17584. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  17585. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  17586. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  17587. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  17588. layout->row.templates[layout->row.columns++] = -1.0f;
  17589. }
  17590. NK_API void
  17591. nk_layout_row_template_push_variable(struct nk_context *ctx, float min_width)
  17592. {
  17593. struct nk_window *win;
  17594. struct nk_panel *layout;
  17595. NK_ASSERT(ctx);
  17596. NK_ASSERT(ctx->current);
  17597. NK_ASSERT(ctx->current->layout);
  17598. if (!ctx || !ctx->current || !ctx->current->layout)
  17599. return;
  17600. win = ctx->current;
  17601. layout = win->layout;
  17602. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  17603. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  17604. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  17605. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  17606. layout->row.templates[layout->row.columns++] = -min_width;
  17607. }
  17608. NK_API void
  17609. nk_layout_row_template_push_static(struct nk_context *ctx, float width)
  17610. {
  17611. struct nk_window *win;
  17612. struct nk_panel *layout;
  17613. NK_ASSERT(ctx);
  17614. NK_ASSERT(ctx->current);
  17615. NK_ASSERT(ctx->current->layout);
  17616. if (!ctx || !ctx->current || !ctx->current->layout)
  17617. return;
  17618. win = ctx->current;
  17619. layout = win->layout;
  17620. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  17621. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  17622. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  17623. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  17624. layout->row.templates[layout->row.columns++] = width;
  17625. }
  17626. NK_API void
  17627. nk_layout_row_template_end(struct nk_context *ctx)
  17628. {
  17629. struct nk_window *win;
  17630. struct nk_panel *layout;
  17631. int i = 0;
  17632. int variable_count = 0;
  17633. int min_variable_count = 0;
  17634. float min_fixed_width = 0.0f;
  17635. float total_fixed_width = 0.0f;
  17636. float max_variable_width = 0.0f;
  17637. NK_ASSERT(ctx);
  17638. NK_ASSERT(ctx->current);
  17639. NK_ASSERT(ctx->current->layout);
  17640. if (!ctx || !ctx->current || !ctx->current->layout)
  17641. return;
  17642. win = ctx->current;
  17643. layout = win->layout;
  17644. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  17645. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  17646. for (i = 0; i < layout->row.columns; ++i) {
  17647. float width = layout->row.templates[i];
  17648. if (width >= 0.0f) {
  17649. total_fixed_width += width;
  17650. min_fixed_width += width;
  17651. } else if (width < -1.0f) {
  17652. width = -width;
  17653. total_fixed_width += width;
  17654. max_variable_width = NK_MAX(max_variable_width, width);
  17655. variable_count++;
  17656. } else {
  17657. min_variable_count++;
  17658. variable_count++;
  17659. }
  17660. }
  17661. if (variable_count) {
  17662. float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  17663. layout->bounds.w, layout->row.columns);
  17664. float var_width = (NK_MAX(space-min_fixed_width,0.0f)) / (float)variable_count;
  17665. int enough_space = var_width >= max_variable_width;
  17666. if (!enough_space)
  17667. var_width = (NK_MAX(space-total_fixed_width,0)) / (float)min_variable_count;
  17668. for (i = 0; i < layout->row.columns; ++i) {
  17669. float *width = &layout->row.templates[i];
  17670. *width = (*width >= 0.0f)? *width: (*width < -1.0f && !enough_space)? -(*width): var_width;
  17671. }
  17672. }
  17673. }
  17674. NK_API void
  17675. nk_layout_space_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  17676. float height, int widget_count)
  17677. {
  17678. struct nk_window *win;
  17679. struct nk_panel *layout;
  17680. NK_ASSERT(ctx);
  17681. NK_ASSERT(ctx->current);
  17682. NK_ASSERT(ctx->current->layout);
  17683. if (!ctx || !ctx->current || !ctx->current->layout)
  17684. return;
  17685. win = ctx->current;
  17686. layout = win->layout;
  17687. nk_panel_layout(ctx, win, height, widget_count);
  17688. if (fmt == NK_STATIC)
  17689. layout->row.type = NK_LAYOUT_STATIC_FREE;
  17690. else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
  17691. layout->row.ratio = 0;
  17692. layout->row.filled = 0;
  17693. layout->row.item_width = 0;
  17694. layout->row.item_offset = 0;
  17695. }
  17696. NK_API void
  17697. nk_layout_space_end(struct nk_context *ctx)
  17698. {
  17699. struct nk_window *win;
  17700. struct nk_panel *layout;
  17701. NK_ASSERT(ctx);
  17702. NK_ASSERT(ctx->current);
  17703. NK_ASSERT(ctx->current->layout);
  17704. if (!ctx || !ctx->current || !ctx->current->layout)
  17705. return;
  17706. win = ctx->current;
  17707. layout = win->layout;
  17708. layout->row.item_width = 0;
  17709. layout->row.item_height = 0;
  17710. layout->row.item_offset = 0;
  17711. nk_zero(&layout->row.item, sizeof(layout->row.item));
  17712. }
  17713. NK_API void
  17714. nk_layout_space_push(struct nk_context *ctx, struct nk_rect rect)
  17715. {
  17716. struct nk_window *win;
  17717. struct nk_panel *layout;
  17718. NK_ASSERT(ctx);
  17719. NK_ASSERT(ctx->current);
  17720. NK_ASSERT(ctx->current->layout);
  17721. if (!ctx || !ctx->current || !ctx->current->layout)
  17722. return;
  17723. win = ctx->current;
  17724. layout = win->layout;
  17725. layout->row.item = rect;
  17726. }
  17727. NK_API struct nk_rect
  17728. nk_layout_space_bounds(struct nk_context *ctx)
  17729. {
  17730. struct nk_rect ret;
  17731. struct nk_window *win;
  17732. struct nk_panel *layout;
  17733. NK_ASSERT(ctx);
  17734. NK_ASSERT(ctx->current);
  17735. NK_ASSERT(ctx->current->layout);
  17736. win = ctx->current;
  17737. layout = win->layout;
  17738. ret.x = layout->clip.x;
  17739. ret.y = layout->clip.y;
  17740. ret.w = layout->clip.w;
  17741. ret.h = layout->row.height;
  17742. return ret;
  17743. }
  17744. NK_API struct nk_rect
  17745. nk_layout_widget_bounds(struct nk_context *ctx)
  17746. {
  17747. struct nk_rect ret;
  17748. struct nk_window *win;
  17749. struct nk_panel *layout;
  17750. NK_ASSERT(ctx);
  17751. NK_ASSERT(ctx->current);
  17752. NK_ASSERT(ctx->current->layout);
  17753. win = ctx->current;
  17754. layout = win->layout;
  17755. ret.x = layout->at_x;
  17756. ret.y = layout->at_y;
  17757. ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x,0);
  17758. ret.h = layout->row.height;
  17759. return ret;
  17760. }
  17761. NK_API struct nk_vec2
  17762. nk_layout_space_to_screen(struct nk_context *ctx, struct nk_vec2 ret)
  17763. {
  17764. struct nk_window *win;
  17765. struct nk_panel *layout;
  17766. NK_ASSERT(ctx);
  17767. NK_ASSERT(ctx->current);
  17768. NK_ASSERT(ctx->current->layout);
  17769. win = ctx->current;
  17770. layout = win->layout;
  17771. ret.x += layout->at_x - (float)*layout->offset_x;
  17772. ret.y += layout->at_y - (float)*layout->offset_y;
  17773. return ret;
  17774. }
  17775. NK_API struct nk_vec2
  17776. nk_layout_space_to_local(struct nk_context *ctx, struct nk_vec2 ret)
  17777. {
  17778. struct nk_window *win;
  17779. struct nk_panel *layout;
  17780. NK_ASSERT(ctx);
  17781. NK_ASSERT(ctx->current);
  17782. NK_ASSERT(ctx->current->layout);
  17783. win = ctx->current;
  17784. layout = win->layout;
  17785. ret.x += -layout->at_x + (float)*layout->offset_x;
  17786. ret.y += -layout->at_y + (float)*layout->offset_y;
  17787. return ret;
  17788. }
  17789. NK_API struct nk_rect
  17790. nk_layout_space_rect_to_screen(struct nk_context *ctx, struct nk_rect ret)
  17791. {
  17792. struct nk_window *win;
  17793. struct nk_panel *layout;
  17794. NK_ASSERT(ctx);
  17795. NK_ASSERT(ctx->current);
  17796. NK_ASSERT(ctx->current->layout);
  17797. win = ctx->current;
  17798. layout = win->layout;
  17799. ret.x += layout->at_x - (float)*layout->offset_x;
  17800. ret.y += layout->at_y - (float)*layout->offset_y;
  17801. return ret;
  17802. }
  17803. NK_API struct nk_rect
  17804. nk_layout_space_rect_to_local(struct nk_context *ctx, struct nk_rect ret)
  17805. {
  17806. struct nk_window *win;
  17807. struct nk_panel *layout;
  17808. NK_ASSERT(ctx);
  17809. NK_ASSERT(ctx->current);
  17810. NK_ASSERT(ctx->current->layout);
  17811. win = ctx->current;
  17812. layout = win->layout;
  17813. ret.x += -layout->at_x + (float)*layout->offset_x;
  17814. ret.y += -layout->at_y + (float)*layout->offset_y;
  17815. return ret;
  17816. }
  17817. NK_INTERN void
  17818. nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win)
  17819. {
  17820. struct nk_panel *layout = win->layout;
  17821. struct nk_vec2 spacing = ctx->style.window.spacing;
  17822. const float row_height = layout->row.height - spacing.y;
  17823. nk_panel_layout(ctx, win, row_height, layout->row.columns);
  17824. }
  17825. NK_INTERN void
  17826. nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx,
  17827. struct nk_window *win, int modify)
  17828. {
  17829. struct nk_panel *layout;
  17830. const struct nk_style *style;
  17831. struct nk_vec2 spacing;
  17832. struct nk_vec2 padding;
  17833. float item_offset = 0;
  17834. float item_width = 0;
  17835. float item_spacing = 0;
  17836. float panel_space = 0;
  17837. NK_ASSERT(ctx);
  17838. NK_ASSERT(ctx->current);
  17839. NK_ASSERT(ctx->current->layout);
  17840. if (!ctx || !ctx->current || !ctx->current->layout)
  17841. return;
  17842. win = ctx->current;
  17843. layout = win->layout;
  17844. style = &ctx->style;
  17845. NK_ASSERT(bounds);
  17846. spacing = style->window.spacing;
  17847. padding = nk_panel_get_padding(style, layout->type);
  17848. panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  17849. layout->bounds.w, layout->row.columns);
  17850. /* calculate the width of one item inside the current layout space */
  17851. switch (layout->row.type) {
  17852. case NK_LAYOUT_DYNAMIC_FIXED: {
  17853. /* scaling fixed size widgets item width */
  17854. item_width = NK_MAX(1.0f,panel_space-1.0f) / (float)layout->row.columns;
  17855. item_offset = (float)layout->row.index * item_width;
  17856. item_spacing = (float)layout->row.index * spacing.x;
  17857. } break;
  17858. case NK_LAYOUT_DYNAMIC_ROW: {
  17859. /* scaling single ratio widget width */
  17860. item_width = layout->row.item_width * panel_space;
  17861. item_offset = layout->row.item_offset;
  17862. item_spacing = 0;
  17863. if (modify) {
  17864. layout->row.item_offset += item_width + spacing.x;
  17865. layout->row.filled += layout->row.item_width;
  17866. layout->row.index = 0;
  17867. }
  17868. } break;
  17869. case NK_LAYOUT_DYNAMIC_FREE: {
  17870. /* panel width depended free widget placing */
  17871. bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
  17872. bounds->x -= (float)*layout->offset_x;
  17873. bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
  17874. bounds->y -= (float)*layout->offset_y;
  17875. bounds->w = layout->bounds.w * layout->row.item.w;
  17876. bounds->h = layout->row.height * layout->row.item.h;
  17877. return;
  17878. } break;
  17879. case NK_LAYOUT_DYNAMIC: {
  17880. /* scaling arrays of panel width ratios for every widget */
  17881. float ratio;
  17882. NK_ASSERT(layout->row.ratio);
  17883. ratio = (layout->row.ratio[layout->row.index] < 0) ?
  17884. layout->row.item_width : layout->row.ratio[layout->row.index];
  17885. item_spacing = (float)layout->row.index * spacing.x;
  17886. item_width = (ratio * panel_space);
  17887. item_offset = layout->row.item_offset;
  17888. if (modify) {
  17889. layout->row.item_offset += item_width;
  17890. layout->row.filled += ratio;
  17891. }
  17892. } break;
  17893. case NK_LAYOUT_STATIC_FIXED: {
  17894. /* non-scaling fixed widgets item width */
  17895. item_width = layout->row.item_width;
  17896. item_offset = (float)layout->row.index * item_width;
  17897. item_spacing = (float)layout->row.index * spacing.x;
  17898. } break;
  17899. case NK_LAYOUT_STATIC_ROW: {
  17900. /* scaling single ratio widget width */
  17901. item_width = layout->row.item_width;
  17902. item_offset = layout->row.item_offset;
  17903. item_spacing = (float)layout->row.index * spacing.x;
  17904. if (modify) layout->row.item_offset += item_width;
  17905. } break;
  17906. case NK_LAYOUT_STATIC_FREE: {
  17907. /* free widget placing */
  17908. bounds->x = layout->at_x + layout->row.item.x;
  17909. bounds->w = layout->row.item.w;
  17910. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  17911. layout->max_x = (bounds->x + bounds->w);
  17912. bounds->x -= (float)*layout->offset_x;
  17913. bounds->y = layout->at_y + layout->row.item.y;
  17914. bounds->y -= (float)*layout->offset_y;
  17915. bounds->h = layout->row.item.h;
  17916. return;
  17917. } break;
  17918. case NK_LAYOUT_STATIC: {
  17919. /* non-scaling array of panel pixel width for every widget */
  17920. item_spacing = (float)layout->row.index * spacing.x;
  17921. item_width = layout->row.ratio[layout->row.index];
  17922. item_offset = layout->row.item_offset;
  17923. if (modify) layout->row.item_offset += item_width;
  17924. } break;
  17925. case NK_LAYOUT_TEMPLATE: {
  17926. /* stretchy row layout with combined dynamic/static widget width*/
  17927. NK_ASSERT(layout->row.index < layout->row.columns);
  17928. NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  17929. item_width = layout->row.templates[layout->row.index];
  17930. item_offset = layout->row.item_offset;
  17931. item_spacing = (float)layout->row.index * spacing.x;
  17932. if (modify) layout->row.item_offset += item_width;
  17933. } break;
  17934. default: NK_ASSERT(0); break;
  17935. };
  17936. /* set the bounds of the newly allocated widget */
  17937. bounds->w = item_width;
  17938. bounds->h = layout->row.height - spacing.y;
  17939. bounds->y = layout->at_y - (float)*layout->offset_y;
  17940. bounds->x = layout->at_x + item_offset + item_spacing + padding.x;
  17941. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  17942. layout->max_x = bounds->x + bounds->w;
  17943. bounds->x -= (float)*layout->offset_x;
  17944. }
  17945. NK_INTERN void
  17946. nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx)
  17947. {
  17948. struct nk_window *win;
  17949. struct nk_panel *layout;
  17950. NK_ASSERT(ctx);
  17951. NK_ASSERT(ctx->current);
  17952. NK_ASSERT(ctx->current->layout);
  17953. if (!ctx || !ctx->current || !ctx->current->layout)
  17954. return;
  17955. /* check if the end of the row has been hit and begin new row if so */
  17956. win = ctx->current;
  17957. layout = win->layout;
  17958. if (layout->row.index >= layout->row.columns)
  17959. nk_panel_alloc_row(ctx, win);
  17960. /* calculate widget position and size */
  17961. nk_layout_widget_space(bounds, ctx, win, nk_true);
  17962. layout->row.index++;
  17963. }
  17964. NK_INTERN void
  17965. nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx)
  17966. {
  17967. float y;
  17968. int index;
  17969. struct nk_window *win;
  17970. struct nk_panel *layout;
  17971. NK_ASSERT(ctx);
  17972. NK_ASSERT(ctx->current);
  17973. NK_ASSERT(ctx->current->layout);
  17974. if (!ctx || !ctx->current || !ctx->current->layout)
  17975. return;
  17976. win = ctx->current;
  17977. layout = win->layout;
  17978. y = layout->at_y;
  17979. index = layout->row.index;
  17980. if (layout->row.index >= layout->row.columns) {
  17981. layout->at_y += layout->row.height;
  17982. layout->row.index = 0;
  17983. }
  17984. nk_layout_widget_space(bounds, ctx, win, nk_false);
  17985. layout->at_y = y;
  17986. layout->row.index = index;
  17987. }
  17988. NK_INTERN int
  17989. nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
  17990. struct nk_image *img, const char *title, enum nk_collapse_states *state)
  17991. {
  17992. struct nk_window *win;
  17993. struct nk_panel *layout;
  17994. const struct nk_style *style;
  17995. struct nk_command_buffer *out;
  17996. const struct nk_input *in;
  17997. const struct nk_style_button *button;
  17998. enum nk_symbol_type symbol;
  17999. float row_height;
  18000. struct nk_vec2 item_spacing;
  18001. struct nk_rect header = {0,0,0,0};
  18002. struct nk_rect sym = {0,0,0,0};
  18003. struct nk_text text;
  18004. nk_flags ws = 0;
  18005. enum nk_widget_layout_states widget_state;
  18006. NK_ASSERT(ctx);
  18007. NK_ASSERT(ctx->current);
  18008. NK_ASSERT(ctx->current->layout);
  18009. if (!ctx || !ctx->current || !ctx->current->layout)
  18010. return 0;
  18011. /* cache some data */
  18012. win = ctx->current;
  18013. layout = win->layout;
  18014. out = &win->buffer;
  18015. style = &ctx->style;
  18016. item_spacing = style->window.spacing;
  18017. /* calculate header bounds and draw background */
  18018. row_height = style->font->height + 2 * style->tab.padding.y;
  18019. nk_layout_set_min_row_height(ctx, row_height);
  18020. nk_layout_row_dynamic(ctx, row_height, 1);
  18021. nk_layout_reset_min_row_height(ctx);
  18022. widget_state = nk_widget(&header, ctx);
  18023. if (type == NK_TREE_TAB) {
  18024. const struct nk_style_item *background = &style->tab.background;
  18025. if (background->type == NK_STYLE_ITEM_IMAGE) {
  18026. nk_draw_image(out, header, &background->data.image, nk_white);
  18027. text.background = nk_rgba(0,0,0,0);
  18028. } else {
  18029. text.background = background->data.color;
  18030. nk_fill_rect(out, header, 0, style->tab.border_color);
  18031. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  18032. style->tab.rounding, background->data.color);
  18033. }
  18034. } else text.background = style->window.background;
  18035. /* update node state */
  18036. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  18037. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  18038. if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
  18039. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
  18040. /* select correct button style */
  18041. if (*state == NK_MAXIMIZED) {
  18042. symbol = style->tab.sym_maximize;
  18043. if (type == NK_TREE_TAB)
  18044. button = &style->tab.tab_maximize_button;
  18045. else button = &style->tab.node_maximize_button;
  18046. } else {
  18047. symbol = style->tab.sym_minimize;
  18048. if (type == NK_TREE_TAB)
  18049. button = &style->tab.tab_minimize_button;
  18050. else button = &style->tab.node_minimize_button;
  18051. }
  18052. {/* draw triangle button */
  18053. sym.w = sym.h = style->font->height;
  18054. sym.y = header.y + style->tab.padding.y;
  18055. sym.x = header.x + style->tab.padding.x;
  18056. nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
  18057. button, 0, style->font);
  18058. if (img) {
  18059. /* draw optional image icon */
  18060. sym.x = sym.x + sym.w + 4 * item_spacing.x;
  18061. nk_draw_image(&win->buffer, sym, img, nk_white);
  18062. sym.w = style->font->height + style->tab.spacing.x;}
  18063. }
  18064. {/* draw label */
  18065. struct nk_rect label;
  18066. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  18067. label.x = sym.x + sym.w + item_spacing.x;
  18068. label.y = sym.y;
  18069. label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
  18070. label.h = style->font->height;
  18071. text.text = style->tab.text;
  18072. text.padding = nk_vec2(0,0);
  18073. nk_widget_text(out, label, title, nk_strlen(title), &text,
  18074. NK_TEXT_LEFT, style->font);}
  18075. /* increase x-axis cursor widget position pointer */
  18076. if (*state == NK_MAXIMIZED) {
  18077. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  18078. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  18079. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  18080. layout->row.tree_depth++;
  18081. return nk_true;
  18082. } else return nk_false;
  18083. }
  18084. NK_INTERN int
  18085. nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
  18086. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  18087. const char *hash, int len, int line)
  18088. {
  18089. struct nk_window *win = ctx->current;
  18090. int title_len = 0;
  18091. nk_hash tree_hash = 0;
  18092. nk_uint *state = 0;
  18093. /* retrieve tree state from internal widget state tables */
  18094. if (!hash) {
  18095. title_len = (int)nk_strlen(title);
  18096. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  18097. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  18098. state = nk_find_value(win, tree_hash);
  18099. if (!state) {
  18100. state = nk_add_value(ctx, win, tree_hash, 0);
  18101. *state = initial_state;
  18102. }
  18103. return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
  18104. }
  18105. NK_API int
  18106. nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
  18107. const char *title, enum nk_collapse_states *state)
  18108. {return nk_tree_state_base(ctx, type, 0, title, state);}
  18109. NK_API int
  18110. nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
  18111. struct nk_image img, const char *title, enum nk_collapse_states *state)
  18112. {return nk_tree_state_base(ctx, type, &img, title, state);}
  18113. NK_API void
  18114. nk_tree_state_pop(struct nk_context *ctx)
  18115. {
  18116. struct nk_window *win = 0;
  18117. struct nk_panel *layout = 0;
  18118. NK_ASSERT(ctx);
  18119. NK_ASSERT(ctx->current);
  18120. NK_ASSERT(ctx->current->layout);
  18121. if (!ctx || !ctx->current || !ctx->current->layout)
  18122. return;
  18123. win = ctx->current;
  18124. layout = win->layout;
  18125. layout->at_x -= ctx->style.tab.indent + ctx->style.window.padding.x;
  18126. layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
  18127. NK_ASSERT(layout->row.tree_depth);
  18128. layout->row.tree_depth--;
  18129. }
  18130. NK_API int
  18131. nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  18132. const char *title, enum nk_collapse_states initial_state,
  18133. const char *hash, int len, int line)
  18134. {return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);}
  18135. NK_API int
  18136. nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  18137. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  18138. const char *hash, int len,int seed)
  18139. {return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);}
  18140. NK_API void
  18141. nk_tree_pop(struct nk_context *ctx)
  18142. {nk_tree_state_pop(ctx);}
  18143. /*----------------------------------------------------------------
  18144. *
  18145. * WIDGETS
  18146. *
  18147. * --------------------------------------------------------------*/
  18148. NK_API struct nk_rect
  18149. nk_widget_bounds(struct nk_context *ctx)
  18150. {
  18151. struct nk_rect bounds;
  18152. NK_ASSERT(ctx);
  18153. NK_ASSERT(ctx->current);
  18154. if (!ctx || !ctx->current)
  18155. return nk_rect(0,0,0,0);
  18156. nk_layout_peek(&bounds, ctx);
  18157. return bounds;
  18158. }
  18159. NK_API struct nk_vec2
  18160. nk_widget_position(struct nk_context *ctx)
  18161. {
  18162. struct nk_rect bounds;
  18163. NK_ASSERT(ctx);
  18164. NK_ASSERT(ctx->current);
  18165. if (!ctx || !ctx->current)
  18166. return nk_vec2(0,0);
  18167. nk_layout_peek(&bounds, ctx);
  18168. return nk_vec2(bounds.x, bounds.y);
  18169. }
  18170. NK_API struct nk_vec2
  18171. nk_widget_size(struct nk_context *ctx)
  18172. {
  18173. struct nk_rect bounds;
  18174. NK_ASSERT(ctx);
  18175. NK_ASSERT(ctx->current);
  18176. if (!ctx || !ctx->current)
  18177. return nk_vec2(0,0);
  18178. nk_layout_peek(&bounds, ctx);
  18179. return nk_vec2(bounds.w, bounds.h);
  18180. }
  18181. NK_API float
  18182. nk_widget_width(struct nk_context *ctx)
  18183. {
  18184. struct nk_rect bounds;
  18185. NK_ASSERT(ctx);
  18186. NK_ASSERT(ctx->current);
  18187. if (!ctx || !ctx->current)
  18188. return 0;
  18189. nk_layout_peek(&bounds, ctx);
  18190. return bounds.w;
  18191. }
  18192. NK_API float
  18193. nk_widget_height(struct nk_context *ctx)
  18194. {
  18195. struct nk_rect bounds;
  18196. NK_ASSERT(ctx);
  18197. NK_ASSERT(ctx->current);
  18198. if (!ctx || !ctx->current)
  18199. return 0;
  18200. nk_layout_peek(&bounds, ctx);
  18201. return bounds.h;
  18202. }
  18203. NK_API int
  18204. nk_widget_is_hovered(struct nk_context *ctx)
  18205. {
  18206. struct nk_rect c, v;
  18207. struct nk_rect bounds;
  18208. NK_ASSERT(ctx);
  18209. NK_ASSERT(ctx->current);
  18210. if (!ctx || !ctx->current || ctx->active != ctx->current)
  18211. return 0;
  18212. c = ctx->current->layout->clip;
  18213. c.x = (float)((int)c.x);
  18214. c.y = (float)((int)c.y);
  18215. c.w = (float)((int)c.w);
  18216. c.h = (float)((int)c.h);
  18217. nk_layout_peek(&bounds, ctx);
  18218. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  18219. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  18220. return 0;
  18221. return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
  18222. }
  18223. NK_API int
  18224. nk_widget_is_mouse_clicked(struct nk_context *ctx, enum nk_buttons btn)
  18225. {
  18226. struct nk_rect c, v;
  18227. struct nk_rect bounds;
  18228. NK_ASSERT(ctx);
  18229. NK_ASSERT(ctx->current);
  18230. if (!ctx || !ctx->current || ctx->active != ctx->current)
  18231. return 0;
  18232. c = ctx->current->layout->clip;
  18233. c.x = (float)((int)c.x);
  18234. c.y = (float)((int)c.y);
  18235. c.w = (float)((int)c.w);
  18236. c.h = (float)((int)c.h);
  18237. nk_layout_peek(&bounds, ctx);
  18238. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  18239. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  18240. return 0;
  18241. return nk_input_mouse_clicked(&ctx->input, btn, bounds);
  18242. }
  18243. NK_API int
  18244. nk_widget_has_mouse_click_down(struct nk_context *ctx, enum nk_buttons btn, int down)
  18245. {
  18246. struct nk_rect c, v;
  18247. struct nk_rect bounds;
  18248. NK_ASSERT(ctx);
  18249. NK_ASSERT(ctx->current);
  18250. if (!ctx || !ctx->current || ctx->active != ctx->current)
  18251. return 0;
  18252. c = ctx->current->layout->clip;
  18253. c.x = (float)((int)c.x);
  18254. c.y = (float)((int)c.y);
  18255. c.w = (float)((int)c.w);
  18256. c.h = (float)((int)c.h);
  18257. nk_layout_peek(&bounds, ctx);
  18258. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  18259. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  18260. return 0;
  18261. return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
  18262. }
  18263. NK_API enum nk_widget_layout_states
  18264. nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
  18265. {
  18266. struct nk_rect c, v;
  18267. struct nk_window *win;
  18268. struct nk_panel *layout;
  18269. const struct nk_input *in;
  18270. NK_ASSERT(ctx);
  18271. NK_ASSERT(ctx->current);
  18272. NK_ASSERT(ctx->current->layout);
  18273. if (!ctx || !ctx->current || !ctx->current->layout)
  18274. return NK_WIDGET_INVALID;
  18275. /* allocate space and check if the widget needs to be updated and drawn */
  18276. nk_panel_alloc_space(bounds, ctx);
  18277. win = ctx->current;
  18278. layout = win->layout;
  18279. in = &ctx->input;
  18280. c = layout->clip;
  18281. /* if one of these triggers you forgot to add an `if` condition around either
  18282. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  18283. Example:
  18284. if (nk_begin(...) {...} nk_end(...); or
  18285. if (nk_group_begin(...) { nk_group_end(...);} */
  18286. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  18287. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  18288. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  18289. /* need to convert to int here to remove floating point errors */
  18290. bounds->x = (float)((int)bounds->x);
  18291. bounds->y = (float)((int)bounds->y);
  18292. bounds->w = (float)((int)bounds->w);
  18293. bounds->h = (float)((int)bounds->h);
  18294. c.x = (float)((int)c.x);
  18295. c.y = (float)((int)c.y);
  18296. c.w = (float)((int)c.w);
  18297. c.h = (float)((int)c.h);
  18298. nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
  18299. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
  18300. return NK_WIDGET_INVALID;
  18301. if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
  18302. return NK_WIDGET_ROM;
  18303. return NK_WIDGET_VALID;
  18304. }
  18305. NK_API enum nk_widget_layout_states
  18306. nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx,
  18307. struct nk_vec2 item_padding)
  18308. {
  18309. /* update the bounds to stand without padding */
  18310. struct nk_window *win;
  18311. struct nk_style *style;
  18312. struct nk_panel *layout;
  18313. enum nk_widget_layout_states state;
  18314. struct nk_vec2 panel_padding;
  18315. NK_ASSERT(ctx);
  18316. NK_ASSERT(ctx->current);
  18317. NK_ASSERT(ctx->current->layout);
  18318. if (!ctx || !ctx->current || !ctx->current->layout)
  18319. return NK_WIDGET_INVALID;
  18320. win = ctx->current;
  18321. style = &ctx->style;
  18322. layout = win->layout;
  18323. state = nk_widget(bounds, ctx);
  18324. panel_padding = nk_panel_get_padding(style, layout->type);
  18325. if (layout->row.index == 1) {
  18326. bounds->w += panel_padding.x;
  18327. bounds->x -= panel_padding.x;
  18328. } else bounds->x -= item_padding.x;
  18329. if (layout->row.index == layout->row.columns)
  18330. bounds->w += panel_padding.x;
  18331. else bounds->w += item_padding.x;
  18332. return state;
  18333. }
  18334. /*----------------------------------------------------------------
  18335. *
  18336. * MISC
  18337. *
  18338. * --------------------------------------------------------------*/
  18339. NK_API void
  18340. nk_spacing(struct nk_context *ctx, int cols)
  18341. {
  18342. struct nk_window *win;
  18343. struct nk_panel *layout;
  18344. struct nk_rect none;
  18345. int i, index, rows;
  18346. NK_ASSERT(ctx);
  18347. NK_ASSERT(ctx->current);
  18348. NK_ASSERT(ctx->current->layout);
  18349. if (!ctx || !ctx->current || !ctx->current->layout)
  18350. return;
  18351. /* spacing over row boundaries */
  18352. win = ctx->current;
  18353. layout = win->layout;
  18354. index = (layout->row.index + cols) % layout->row.columns;
  18355. rows = (layout->row.index + cols) / layout->row.columns;
  18356. if (rows) {
  18357. for (i = 0; i < rows; ++i)
  18358. nk_panel_alloc_row(ctx, win);
  18359. cols = index;
  18360. }
  18361. /* non table layout need to allocate space */
  18362. if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
  18363. layout->row.type != NK_LAYOUT_STATIC_FIXED) {
  18364. for (i = 0; i < cols; ++i)
  18365. nk_panel_alloc_space(&none, ctx);
  18366. }
  18367. layout->row.index = index;
  18368. }
  18369. /*----------------------------------------------------------------
  18370. *
  18371. * TEXT
  18372. *
  18373. * --------------------------------------------------------------*/
  18374. NK_API void
  18375. nk_text_colored(struct nk_context *ctx, const char *str, int len,
  18376. nk_flags alignment, struct nk_color color)
  18377. {
  18378. struct nk_window *win;
  18379. const struct nk_style *style;
  18380. struct nk_vec2 item_padding;
  18381. struct nk_rect bounds;
  18382. struct nk_text text;
  18383. NK_ASSERT(ctx);
  18384. NK_ASSERT(ctx->current);
  18385. NK_ASSERT(ctx->current->layout);
  18386. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18387. win = ctx->current;
  18388. style = &ctx->style;
  18389. nk_panel_alloc_space(&bounds, ctx);
  18390. item_padding = style->text.padding;
  18391. text.padding.x = item_padding.x;
  18392. text.padding.y = item_padding.y;
  18393. text.background = style->window.background;
  18394. text.text = color;
  18395. nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
  18396. }
  18397. NK_API void
  18398. nk_text_wrap_colored(struct nk_context *ctx, const char *str,
  18399. int len, struct nk_color color)
  18400. {
  18401. struct nk_window *win;
  18402. const struct nk_style *style;
  18403. struct nk_vec2 item_padding;
  18404. struct nk_rect bounds;
  18405. struct nk_text text;
  18406. NK_ASSERT(ctx);
  18407. NK_ASSERT(ctx->current);
  18408. NK_ASSERT(ctx->current->layout);
  18409. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18410. win = ctx->current;
  18411. style = &ctx->style;
  18412. nk_panel_alloc_space(&bounds, ctx);
  18413. item_padding = style->text.padding;
  18414. text.padding.x = item_padding.x;
  18415. text.padding.y = item_padding.y;
  18416. text.background = style->window.background;
  18417. text.text = color;
  18418. nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
  18419. }
  18420. #ifdef NK_INCLUDE_STANDARD_VARARGS
  18421. NK_API void
  18422. nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
  18423. struct nk_color color, const char *fmt, ...)
  18424. {
  18425. char buf[256];
  18426. va_list args;
  18427. va_start(args, fmt);
  18428. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18429. nk_label_colored(ctx, buf, flags, color);
  18430. va_end(args);
  18431. }
  18432. NK_API void
  18433. nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
  18434. const char *fmt, ...)
  18435. {
  18436. char buf[256];
  18437. va_list args;
  18438. va_start(args, fmt);
  18439. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18440. nk_label_colored_wrap(ctx, buf, color);
  18441. va_end(args);
  18442. }
  18443. NK_API void
  18444. nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
  18445. {
  18446. char buf[256];
  18447. va_list args;
  18448. va_start(args, fmt);
  18449. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18450. nk_label(ctx, buf, flags);
  18451. va_end(args);
  18452. }
  18453. NK_API void
  18454. nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
  18455. {
  18456. char buf[256];
  18457. va_list args;
  18458. va_start(args, fmt);
  18459. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18460. nk_label_wrap(ctx, buf);
  18461. va_end(args);
  18462. }
  18463. NK_API void
  18464. nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
  18465. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));}
  18466. NK_API void
  18467. nk_value_int(struct nk_context *ctx, const char *prefix, int value)
  18468. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);}
  18469. NK_API void
  18470. nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
  18471. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);}
  18472. NK_API void
  18473. nk_value_float(struct nk_context *ctx, const char *prefix, float value)
  18474. {
  18475. double double_value = (double)value;
  18476. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
  18477. }
  18478. NK_API void
  18479. nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
  18480. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);}
  18481. NK_API void
  18482. nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
  18483. {
  18484. double c[4]; nk_color_dv(c, color);
  18485. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
  18486. p, c[0], c[1], c[2], c[3]);
  18487. }
  18488. NK_API void
  18489. nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
  18490. {
  18491. char hex[16];
  18492. nk_color_hex_rgba(hex, color);
  18493. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
  18494. }
  18495. #endif
  18496. NK_API void
  18497. nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
  18498. {
  18499. NK_ASSERT(ctx);
  18500. if (!ctx) return;
  18501. nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
  18502. }
  18503. NK_API void
  18504. nk_text_wrap(struct nk_context *ctx, const char *str, int len)
  18505. {
  18506. NK_ASSERT(ctx);
  18507. if (!ctx) return;
  18508. nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
  18509. }
  18510. NK_API void
  18511. nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
  18512. {nk_text(ctx, str, nk_strlen(str), alignment);}
  18513. NK_API void
  18514. nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
  18515. struct nk_color color)
  18516. {nk_text_colored(ctx, str, nk_strlen(str), align, color);}
  18517. NK_API void
  18518. nk_label_wrap(struct nk_context *ctx, const char *str)
  18519. {nk_text_wrap(ctx, str, nk_strlen(str));}
  18520. NK_API void
  18521. nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
  18522. {nk_text_wrap_colored(ctx, str, nk_strlen(str), color);}
  18523. NK_API void
  18524. nk_image(struct nk_context *ctx, struct nk_image img)
  18525. {
  18526. struct nk_window *win;
  18527. struct nk_rect bounds;
  18528. NK_ASSERT(ctx);
  18529. NK_ASSERT(ctx->current);
  18530. NK_ASSERT(ctx->current->layout);
  18531. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18532. win = ctx->current;
  18533. if (!nk_widget(&bounds, ctx)) return;
  18534. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  18535. }
  18536. /*----------------------------------------------------------------
  18537. *
  18538. * BUTTON
  18539. *
  18540. * --------------------------------------------------------------*/
  18541. NK_API void
  18542. nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  18543. {
  18544. NK_ASSERT(ctx);
  18545. if (!ctx) return;
  18546. ctx->button_behavior = behavior;
  18547. }
  18548. NK_API int
  18549. nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  18550. {
  18551. struct nk_config_stack_button_behavior *button_stack;
  18552. struct nk_config_stack_button_behavior_element *element;
  18553. NK_ASSERT(ctx);
  18554. if (!ctx) return 0;
  18555. button_stack = &ctx->stacks.button_behaviors;
  18556. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  18557. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  18558. return 0;
  18559. element = &button_stack->elements[button_stack->head++];
  18560. element->address = &ctx->button_behavior;
  18561. element->old_value = ctx->button_behavior;
  18562. ctx->button_behavior = behavior;
  18563. return 1;
  18564. }
  18565. NK_API int
  18566. nk_button_pop_behavior(struct nk_context *ctx)
  18567. {
  18568. struct nk_config_stack_button_behavior *button_stack;
  18569. struct nk_config_stack_button_behavior_element *element;
  18570. NK_ASSERT(ctx);
  18571. if (!ctx) return 0;
  18572. button_stack = &ctx->stacks.button_behaviors;
  18573. NK_ASSERT(button_stack->head > 0);
  18574. if (button_stack->head < 1)
  18575. return 0;
  18576. element = &button_stack->elements[--button_stack->head];
  18577. *element->address = element->old_value;
  18578. return 1;
  18579. }
  18580. NK_API int
  18581. nk_button_text_styled(struct nk_context *ctx,
  18582. const struct nk_style_button *style, const char *title, int len)
  18583. {
  18584. struct nk_window *win;
  18585. struct nk_panel *layout;
  18586. const struct nk_input *in;
  18587. struct nk_rect bounds;
  18588. enum nk_widget_layout_states state;
  18589. NK_ASSERT(ctx);
  18590. NK_ASSERT(style);
  18591. NK_ASSERT(ctx->current);
  18592. NK_ASSERT(ctx->current->layout);
  18593. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  18594. win = ctx->current;
  18595. layout = win->layout;
  18596. state = nk_widget(&bounds, ctx);
  18597. if (!state) return 0;
  18598. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18599. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  18600. title, len, style->text_alignment, ctx->button_behavior,
  18601. style, in, ctx->style.font);
  18602. }
  18603. NK_API int
  18604. nk_button_text(struct nk_context *ctx, const char *title, int len)
  18605. {
  18606. NK_ASSERT(ctx);
  18607. if (!ctx) return 0;
  18608. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  18609. }
  18610. NK_API int nk_button_label_styled(struct nk_context *ctx,
  18611. const struct nk_style_button *style, const char *title)
  18612. {return nk_button_text_styled(ctx, style, title, nk_strlen(title));}
  18613. NK_API int nk_button_label(struct nk_context *ctx, const char *title)
  18614. {return nk_button_text(ctx, title, nk_strlen(title));}
  18615. NK_API int
  18616. nk_button_color(struct nk_context *ctx, struct nk_color color)
  18617. {
  18618. struct nk_window *win;
  18619. struct nk_panel *layout;
  18620. const struct nk_input *in;
  18621. struct nk_style_button button;
  18622. int ret = 0;
  18623. struct nk_rect bounds;
  18624. struct nk_rect content;
  18625. enum nk_widget_layout_states state;
  18626. NK_ASSERT(ctx);
  18627. NK_ASSERT(ctx->current);
  18628. NK_ASSERT(ctx->current->layout);
  18629. if (!ctx || !ctx->current || !ctx->current->layout)
  18630. return 0;
  18631. win = ctx->current;
  18632. layout = win->layout;
  18633. state = nk_widget(&bounds, ctx);
  18634. if (!state) return 0;
  18635. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18636. button = ctx->style.button;
  18637. button.normal = nk_style_item_color(color);
  18638. button.hover = nk_style_item_color(color);
  18639. button.active = nk_style_item_color(color);
  18640. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  18641. &button, in, ctx->button_behavior, &content);
  18642. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  18643. return ret;
  18644. }
  18645. NK_API int
  18646. nk_button_symbol_styled(struct nk_context *ctx,
  18647. const struct nk_style_button *style, enum nk_symbol_type symbol)
  18648. {
  18649. struct nk_window *win;
  18650. struct nk_panel *layout;
  18651. const struct nk_input *in;
  18652. struct nk_rect bounds;
  18653. enum nk_widget_layout_states state;
  18654. NK_ASSERT(ctx);
  18655. NK_ASSERT(ctx->current);
  18656. NK_ASSERT(ctx->current->layout);
  18657. if (!ctx || !ctx->current || !ctx->current->layout)
  18658. return 0;
  18659. win = ctx->current;
  18660. layout = win->layout;
  18661. state = nk_widget(&bounds, ctx);
  18662. if (!state) return 0;
  18663. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18664. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  18665. symbol, ctx->button_behavior, style, in, ctx->style.font);
  18666. }
  18667. NK_API int
  18668. nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
  18669. {
  18670. NK_ASSERT(ctx);
  18671. if (!ctx) return 0;
  18672. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  18673. }
  18674. NK_API int
  18675. nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
  18676. struct nk_image img)
  18677. {
  18678. struct nk_window *win;
  18679. struct nk_panel *layout;
  18680. const struct nk_input *in;
  18681. struct nk_rect bounds;
  18682. enum nk_widget_layout_states state;
  18683. NK_ASSERT(ctx);
  18684. NK_ASSERT(ctx->current);
  18685. NK_ASSERT(ctx->current->layout);
  18686. if (!ctx || !ctx->current || !ctx->current->layout)
  18687. return 0;
  18688. win = ctx->current;
  18689. layout = win->layout;
  18690. state = nk_widget(&bounds, ctx);
  18691. if (!state) return 0;
  18692. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18693. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  18694. img, ctx->button_behavior, style, in);
  18695. }
  18696. NK_API int
  18697. nk_button_image(struct nk_context *ctx, struct nk_image img)
  18698. {
  18699. NK_ASSERT(ctx);
  18700. if (!ctx) return 0;
  18701. return nk_button_image_styled(ctx, &ctx->style.button, img);
  18702. }
  18703. NK_API int
  18704. nk_button_symbol_text_styled(struct nk_context *ctx,
  18705. const struct nk_style_button *style, enum nk_symbol_type symbol,
  18706. const char *text, int len, nk_flags align)
  18707. {
  18708. struct nk_window *win;
  18709. struct nk_panel *layout;
  18710. const struct nk_input *in;
  18711. struct nk_rect bounds;
  18712. enum nk_widget_layout_states state;
  18713. NK_ASSERT(ctx);
  18714. NK_ASSERT(ctx->current);
  18715. NK_ASSERT(ctx->current->layout);
  18716. if (!ctx || !ctx->current || !ctx->current->layout)
  18717. return 0;
  18718. win = ctx->current;
  18719. layout = win->layout;
  18720. state = nk_widget(&bounds, ctx);
  18721. if (!state) return 0;
  18722. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18723. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  18724. symbol, text, len, align, ctx->button_behavior,
  18725. style, ctx->style.font, in);
  18726. }
  18727. NK_API int
  18728. nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  18729. const char* text, int len, nk_flags align)
  18730. {
  18731. NK_ASSERT(ctx);
  18732. if (!ctx) return 0;
  18733. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  18734. }
  18735. NK_API int nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  18736. const char *label, nk_flags align)
  18737. {return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);}
  18738. NK_API int nk_button_symbol_label_styled(struct nk_context *ctx,
  18739. const struct nk_style_button *style, enum nk_symbol_type symbol,
  18740. const char *title, nk_flags align)
  18741. {return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);}
  18742. NK_API int
  18743. nk_button_image_text_styled(struct nk_context *ctx,
  18744. const struct nk_style_button *style, struct nk_image img, const char *text,
  18745. int len, nk_flags align)
  18746. {
  18747. struct nk_window *win;
  18748. struct nk_panel *layout;
  18749. const struct nk_input *in;
  18750. struct nk_rect bounds;
  18751. enum nk_widget_layout_states state;
  18752. NK_ASSERT(ctx);
  18753. NK_ASSERT(ctx->current);
  18754. NK_ASSERT(ctx->current->layout);
  18755. if (!ctx || !ctx->current || !ctx->current->layout)
  18756. return 0;
  18757. win = ctx->current;
  18758. layout = win->layout;
  18759. state = nk_widget(&bounds, ctx);
  18760. if (!state) return 0;
  18761. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18762. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  18763. bounds, img, text, len, align, ctx->button_behavior,
  18764. style, ctx->style.font, in);
  18765. }
  18766. NK_API int
  18767. nk_button_image_text(struct nk_context *ctx, struct nk_image img,
  18768. const char *text, int len, nk_flags align)
  18769. {return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);}
  18770. NK_API int nk_button_image_label(struct nk_context *ctx, struct nk_image img,
  18771. const char *label, nk_flags align)
  18772. {return nk_button_image_text(ctx, img, label, nk_strlen(label), align);}
  18773. NK_API int nk_button_image_label_styled(struct nk_context *ctx,
  18774. const struct nk_style_button *style, struct nk_image img,
  18775. const char *label, nk_flags text_alignment)
  18776. {return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);}
  18777. /*----------------------------------------------------------------
  18778. *
  18779. * SELECTABLE
  18780. *
  18781. * --------------------------------------------------------------*/
  18782. NK_API int
  18783. nk_selectable_text(struct nk_context *ctx, const char *str, int len,
  18784. nk_flags align, int *value)
  18785. {
  18786. struct nk_window *win;
  18787. struct nk_panel *layout;
  18788. const struct nk_input *in;
  18789. const struct nk_style *style;
  18790. enum nk_widget_layout_states state;
  18791. struct nk_rect bounds;
  18792. NK_ASSERT(ctx);
  18793. NK_ASSERT(value);
  18794. NK_ASSERT(ctx->current);
  18795. NK_ASSERT(ctx->current->layout);
  18796. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  18797. return 0;
  18798. win = ctx->current;
  18799. layout = win->layout;
  18800. style = &ctx->style;
  18801. state = nk_widget(&bounds, ctx);
  18802. if (!state) return 0;
  18803. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18804. return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
  18805. str, len, align, value, &style->selectable, in, style->font);
  18806. }
  18807. NK_API int
  18808. nk_selectable_image_text(struct nk_context *ctx, struct nk_image img,
  18809. const char *str, int len, nk_flags align, int *value)
  18810. {
  18811. struct nk_window *win;
  18812. struct nk_panel *layout;
  18813. const struct nk_input *in;
  18814. const struct nk_style *style;
  18815. enum nk_widget_layout_states state;
  18816. struct nk_rect bounds;
  18817. NK_ASSERT(ctx);
  18818. NK_ASSERT(value);
  18819. NK_ASSERT(ctx->current);
  18820. NK_ASSERT(ctx->current->layout);
  18821. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  18822. return 0;
  18823. win = ctx->current;
  18824. layout = win->layout;
  18825. style = &ctx->style;
  18826. state = nk_widget(&bounds, ctx);
  18827. if (!state) return 0;
  18828. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18829. return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
  18830. str, len, align, value, &img, &style->selectable, in, style->font);
  18831. }
  18832. NK_API int nk_select_text(struct nk_context *ctx, const char *str, int len,
  18833. nk_flags align, int value)
  18834. {nk_selectable_text(ctx, str, len, align, &value);return value;}
  18835. NK_API int nk_selectable_label(struct nk_context *ctx, const char *str, nk_flags align, int *value)
  18836. {return nk_selectable_text(ctx, str, nk_strlen(str), align, value);}
  18837. NK_API int nk_selectable_image_label(struct nk_context *ctx,struct nk_image img,
  18838. const char *str, nk_flags align, int *value)
  18839. {return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);}
  18840. NK_API int nk_select_label(struct nk_context *ctx, const char *str, nk_flags align, int value)
  18841. {nk_selectable_text(ctx, str, nk_strlen(str), align, &value);return value;}
  18842. NK_API int nk_select_image_label(struct nk_context *ctx, struct nk_image img,
  18843. const char *str, nk_flags align, int value)
  18844. {nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value);return value;}
  18845. NK_API int nk_select_image_text(struct nk_context *ctx, struct nk_image img,
  18846. const char *str, int len, nk_flags align, int value)
  18847. {nk_selectable_image_text(ctx, img, str, len, align, &value);return value;}
  18848. /*----------------------------------------------------------------
  18849. *
  18850. * CHECKBOX
  18851. *
  18852. * --------------------------------------------------------------*/
  18853. NK_API int
  18854. nk_check_text(struct nk_context *ctx, const char *text, int len, int active)
  18855. {
  18856. struct nk_window *win;
  18857. struct nk_panel *layout;
  18858. const struct nk_input *in;
  18859. const struct nk_style *style;
  18860. struct nk_rect bounds;
  18861. enum nk_widget_layout_states state;
  18862. NK_ASSERT(ctx);
  18863. NK_ASSERT(ctx->current);
  18864. NK_ASSERT(ctx->current->layout);
  18865. if (!ctx || !ctx->current || !ctx->current->layout)
  18866. return active;
  18867. win = ctx->current;
  18868. style = &ctx->style;
  18869. layout = win->layout;
  18870. state = nk_widget(&bounds, ctx);
  18871. if (!state) return active;
  18872. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18873. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  18874. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
  18875. return active;
  18876. }
  18877. NK_API unsigned int
  18878. nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
  18879. unsigned int flags, unsigned int value)
  18880. {
  18881. int old_active;
  18882. NK_ASSERT(ctx);
  18883. NK_ASSERT(text);
  18884. if (!ctx || !text) return flags;
  18885. old_active = (int)((flags & value) & value);
  18886. if (nk_check_text(ctx, text, len, old_active))
  18887. flags |= value;
  18888. else flags &= ~value;
  18889. return flags;
  18890. }
  18891. NK_API int
  18892. nk_checkbox_text(struct nk_context *ctx, const char *text, int len, int *active)
  18893. {
  18894. int old_val;
  18895. NK_ASSERT(ctx);
  18896. NK_ASSERT(text);
  18897. NK_ASSERT(active);
  18898. if (!ctx || !text || !active) return 0;
  18899. old_val = *active;
  18900. *active = nk_check_text(ctx, text, len, *active);
  18901. return old_val != *active;
  18902. }
  18903. NK_API int
  18904. nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
  18905. unsigned int *flags, unsigned int value)
  18906. {
  18907. int active;
  18908. NK_ASSERT(ctx);
  18909. NK_ASSERT(text);
  18910. NK_ASSERT(flags);
  18911. if (!ctx || !text || !flags) return 0;
  18912. active = (int)((*flags & value) & value);
  18913. if (nk_checkbox_text(ctx, text, len, &active)) {
  18914. if (active) *flags |= value;
  18915. else *flags &= ~value;
  18916. return 1;
  18917. }
  18918. return 0;
  18919. }
  18920. NK_API int nk_check_label(struct nk_context *ctx, const char *label, int active)
  18921. {return nk_check_text(ctx, label, nk_strlen(label), active);}
  18922. NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
  18923. unsigned int flags, unsigned int value)
  18924. {return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);}
  18925. NK_API int nk_checkbox_label(struct nk_context *ctx, const char *label, int *active)
  18926. {return nk_checkbox_text(ctx, label, nk_strlen(label), active);}
  18927. NK_API int nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
  18928. unsigned int *flags, unsigned int value)
  18929. {return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);}
  18930. /*----------------------------------------------------------------
  18931. *
  18932. * OPTION
  18933. *
  18934. * --------------------------------------------------------------*/
  18935. NK_API int
  18936. nk_option_text(struct nk_context *ctx, const char *text, int len, int is_active)
  18937. {
  18938. struct nk_window *win;
  18939. struct nk_panel *layout;
  18940. const struct nk_input *in;
  18941. const struct nk_style *style;
  18942. struct nk_rect bounds;
  18943. enum nk_widget_layout_states state;
  18944. NK_ASSERT(ctx);
  18945. NK_ASSERT(ctx->current);
  18946. NK_ASSERT(ctx->current->layout);
  18947. if (!ctx || !ctx->current || !ctx->current->layout)
  18948. return is_active;
  18949. win = ctx->current;
  18950. style = &ctx->style;
  18951. layout = win->layout;
  18952. state = nk_widget(&bounds, ctx);
  18953. if (!state) return state;
  18954. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18955. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  18956. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
  18957. return is_active;
  18958. }
  18959. NK_API int
  18960. nk_radio_text(struct nk_context *ctx, const char *text, int len, int *active)
  18961. {
  18962. int old_value;
  18963. NK_ASSERT(ctx);
  18964. NK_ASSERT(text);
  18965. NK_ASSERT(active);
  18966. if (!ctx || !text || !active) return 0;
  18967. old_value = *active;
  18968. *active = nk_option_text(ctx, text, len, old_value);
  18969. return old_value != *active;
  18970. }
  18971. NK_API int
  18972. nk_option_label(struct nk_context *ctx, const char *label, int active)
  18973. {return nk_option_text(ctx, label, nk_strlen(label), active);}
  18974. NK_API int
  18975. nk_radio_label(struct nk_context *ctx, const char *label, int *active)
  18976. {return nk_radio_text(ctx, label, nk_strlen(label), active);}
  18977. /*----------------------------------------------------------------
  18978. *
  18979. * SLIDER
  18980. *
  18981. * --------------------------------------------------------------*/
  18982. NK_API int
  18983. nk_slider_float(struct nk_context *ctx, float min_value, float *value, float max_value,
  18984. float value_step)
  18985. {
  18986. struct nk_window *win;
  18987. struct nk_panel *layout;
  18988. struct nk_input *in;
  18989. const struct nk_style *style;
  18990. int ret = 0;
  18991. float old_value;
  18992. struct nk_rect bounds;
  18993. enum nk_widget_layout_states state;
  18994. NK_ASSERT(ctx);
  18995. NK_ASSERT(ctx->current);
  18996. NK_ASSERT(ctx->current->layout);
  18997. NK_ASSERT(value);
  18998. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  18999. return ret;
  19000. win = ctx->current;
  19001. style = &ctx->style;
  19002. layout = win->layout;
  19003. state = nk_widget(&bounds, ctx);
  19004. if (!state) return ret;
  19005. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19006. old_value = *value;
  19007. *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  19008. old_value, max_value, value_step, &style->slider, in, style->font);
  19009. return (old_value > *value || old_value < *value);
  19010. }
  19011. NK_API float
  19012. nk_slide_float(struct nk_context *ctx, float min, float val, float max, float step)
  19013. {
  19014. nk_slider_float(ctx, min, &val, max, step); return val;
  19015. }
  19016. NK_API int
  19017. nk_slide_int(struct nk_context *ctx, int min, int val, int max, int step)
  19018. {
  19019. float value = (float)val;
  19020. nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  19021. return (int)value;
  19022. }
  19023. NK_API int
  19024. nk_slider_int(struct nk_context *ctx, int min, int *val, int max, int step)
  19025. {
  19026. int ret;
  19027. float value = (float)*val;
  19028. ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  19029. *val = (int)value;
  19030. return ret;
  19031. }
  19032. /*----------------------------------------------------------------
  19033. *
  19034. * PROGRESSBAR
  19035. *
  19036. * --------------------------------------------------------------*/
  19037. NK_API int
  19038. nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, int is_modifyable)
  19039. {
  19040. struct nk_window *win;
  19041. struct nk_panel *layout;
  19042. const struct nk_style *style;
  19043. const struct nk_input *in;
  19044. struct nk_rect bounds;
  19045. enum nk_widget_layout_states state;
  19046. nk_size old_value;
  19047. NK_ASSERT(ctx);
  19048. NK_ASSERT(cur);
  19049. NK_ASSERT(ctx->current);
  19050. NK_ASSERT(ctx->current->layout);
  19051. if (!ctx || !ctx->current || !ctx->current->layout || !cur)
  19052. return 0;
  19053. win = ctx->current;
  19054. style = &ctx->style;
  19055. layout = win->layout;
  19056. state = nk_widget(&bounds, ctx);
  19057. if (!state) return 0;
  19058. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19059. old_value = *cur;
  19060. *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
  19061. *cur, max, is_modifyable, &style->progress, in);
  19062. return (*cur != old_value);
  19063. }
  19064. NK_API nk_size nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, int modifyable)
  19065. {nk_progress(ctx, &cur, max, modifyable);return cur;}
  19066. /*----------------------------------------------------------------
  19067. *
  19068. * EDIT
  19069. *
  19070. * --------------------------------------------------------------*/
  19071. NK_API void
  19072. nk_edit_focus(struct nk_context *ctx, nk_flags flags)
  19073. {
  19074. nk_hash hash;
  19075. struct nk_window *win;
  19076. NK_ASSERT(ctx);
  19077. NK_ASSERT(ctx->current);
  19078. if (!ctx || !ctx->current) return;
  19079. win = ctx->current;
  19080. hash = win->edit.seq;
  19081. win->edit.active = nk_true;
  19082. win->edit.name = hash;
  19083. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  19084. win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
  19085. }
  19086. NK_API void
  19087. nk_edit_unfocus(struct nk_context *ctx)
  19088. {
  19089. struct nk_window *win;
  19090. NK_ASSERT(ctx);
  19091. NK_ASSERT(ctx->current);
  19092. if (!ctx || !ctx->current) return;
  19093. win = ctx->current;
  19094. win->edit.active = nk_false;
  19095. win->edit.name = 0;
  19096. }
  19097. NK_API nk_flags
  19098. nk_edit_string(struct nk_context *ctx, nk_flags flags,
  19099. char *memory, int *len, int max, nk_plugin_filter filter)
  19100. {
  19101. nk_hash hash;
  19102. nk_flags state;
  19103. struct nk_text_edit *edit;
  19104. struct nk_window *win;
  19105. NK_ASSERT(ctx);
  19106. NK_ASSERT(memory);
  19107. NK_ASSERT(len);
  19108. if (!ctx || !memory || !len)
  19109. return 0;
  19110. filter = (!filter) ? nk_filter_default: filter;
  19111. win = ctx->current;
  19112. hash = win->edit.seq;
  19113. edit = &ctx->text_edit;
  19114. nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE)?
  19115. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE, filter);
  19116. if (win->edit.active && hash == win->edit.name) {
  19117. if (flags & NK_EDIT_NO_CURSOR)
  19118. edit->cursor = nk_utf_len(memory, *len);
  19119. else edit->cursor = win->edit.cursor;
  19120. if (!(flags & NK_EDIT_SELECTABLE)) {
  19121. edit->select_start = win->edit.cursor;
  19122. edit->select_end = win->edit.cursor;
  19123. } else {
  19124. edit->select_start = win->edit.sel_start;
  19125. edit->select_end = win->edit.sel_end;
  19126. }
  19127. edit->mode = win->edit.mode;
  19128. edit->scrollbar.x = (float)win->edit.scrollbar.x;
  19129. edit->scrollbar.y = (float)win->edit.scrollbar.y;
  19130. edit->active = nk_true;
  19131. } else edit->active = nk_false;
  19132. max = NK_MAX(1, max);
  19133. *len = NK_MIN(*len, max-1);
  19134. nk_str_init_fixed(&edit->string, memory, (nk_size)max);
  19135. edit->string.buffer.allocated = (nk_size)*len;
  19136. edit->string.len = nk_utf_len(memory, *len);
  19137. state = nk_edit_buffer(ctx, flags, edit, filter);
  19138. *len = (int)edit->string.buffer.allocated;
  19139. if (edit->active) {
  19140. win->edit.cursor = edit->cursor;
  19141. win->edit.sel_start = edit->select_start;
  19142. win->edit.sel_end = edit->select_end;
  19143. win->edit.mode = edit->mode;
  19144. win->edit.scrollbar.x = (nk_ushort)edit->scrollbar.x;
  19145. win->edit.scrollbar.y = (nk_ushort)edit->scrollbar.y;
  19146. }
  19147. return state;
  19148. }
  19149. NK_API nk_flags
  19150. nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
  19151. struct nk_text_edit *edit, nk_plugin_filter filter)
  19152. {
  19153. struct nk_window *win;
  19154. struct nk_style *style;
  19155. struct nk_input *in;
  19156. enum nk_widget_layout_states state;
  19157. struct nk_rect bounds;
  19158. nk_flags ret_flags = 0;
  19159. unsigned char prev_state;
  19160. nk_hash hash;
  19161. /* make sure correct values */
  19162. NK_ASSERT(ctx);
  19163. NK_ASSERT(edit);
  19164. NK_ASSERT(ctx->current);
  19165. NK_ASSERT(ctx->current->layout);
  19166. if (!ctx || !ctx->current || !ctx->current->layout)
  19167. return 0;
  19168. win = ctx->current;
  19169. style = &ctx->style;
  19170. state = nk_widget(&bounds, ctx);
  19171. if (!state) return state;
  19172. in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19173. /* check if edit is currently hot item */
  19174. hash = win->edit.seq++;
  19175. if (win->edit.active && hash == win->edit.name) {
  19176. if (flags & NK_EDIT_NO_CURSOR)
  19177. edit->cursor = edit->string.len;
  19178. if (!(flags & NK_EDIT_SELECTABLE)) {
  19179. edit->select_start = edit->cursor;
  19180. edit->select_end = edit->cursor;
  19181. }
  19182. if (flags & NK_EDIT_CLIPBOARD)
  19183. edit->clip = ctx->clip;
  19184. }
  19185. filter = (!filter) ? nk_filter_default: filter;
  19186. prev_state = (unsigned char)edit->active;
  19187. in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
  19188. ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
  19189. filter, edit, &style->edit, in, style->font);
  19190. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  19191. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
  19192. if (edit->active && prev_state != edit->active) {
  19193. /* current edit is now hot */
  19194. win->edit.active = nk_true;
  19195. win->edit.name = hash;
  19196. } else if (prev_state && !edit->active) {
  19197. /* current edit is now cold */
  19198. win->edit.active = nk_false;
  19199. }
  19200. return ret_flags;
  19201. }
  19202. NK_API nk_flags
  19203. nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
  19204. char *buffer, int max, nk_plugin_filter filter)
  19205. {
  19206. nk_flags result;
  19207. int len = nk_strlen(buffer);
  19208. result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
  19209. buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';
  19210. return result;
  19211. }
  19212. /*----------------------------------------------------------------
  19213. *
  19214. * PROPERTY
  19215. *
  19216. * --------------------------------------------------------------*/
  19217. NK_INTERN struct nk_property_variant
  19218. nk_property_variant_int(int value, int min_value, int max_value, int step)
  19219. {
  19220. struct nk_property_variant result;
  19221. result.kind = NK_PROPERTY_INT;
  19222. result.value.i = value;
  19223. result.min_value.i = min_value;
  19224. result.max_value.i = max_value;
  19225. result.step.i = step;
  19226. return result;
  19227. }
  19228. NK_INTERN struct nk_property_variant
  19229. nk_property_variant_float(float value, float min_value, float max_value, float step)
  19230. {
  19231. struct nk_property_variant result;
  19232. result.kind = NK_PROPERTY_FLOAT;
  19233. result.value.f = value;
  19234. result.min_value.f = min_value;
  19235. result.max_value.f = max_value;
  19236. result.step.f = step;
  19237. return result;
  19238. }
  19239. NK_INTERN struct nk_property_variant
  19240. nk_property_variant_double(double value, double min_value, double max_value,
  19241. double step)
  19242. {
  19243. struct nk_property_variant result;
  19244. result.kind = NK_PROPERTY_DOUBLE;
  19245. result.value.d = value;
  19246. result.min_value.d = min_value;
  19247. result.max_value.d = max_value;
  19248. result.step.d = step;
  19249. return result;
  19250. }
  19251. NK_INTERN void
  19252. nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant,
  19253. float inc_per_pixel, const enum nk_property_filter filter)
  19254. {
  19255. struct nk_window *win;
  19256. struct nk_panel *layout;
  19257. struct nk_input *in;
  19258. const struct nk_style *style;
  19259. struct nk_rect bounds;
  19260. enum nk_widget_layout_states s;
  19261. int *state = 0;
  19262. nk_hash hash = 0;
  19263. char *buffer = 0;
  19264. int *len = 0;
  19265. int *cursor = 0;
  19266. int *select_begin = 0;
  19267. int *select_end = 0;
  19268. int old_state;
  19269. char dummy_buffer[NK_MAX_NUMBER_BUFFER];
  19270. int dummy_state = NK_PROPERTY_DEFAULT;
  19271. int dummy_length = 0;
  19272. int dummy_cursor = 0;
  19273. int dummy_select_begin = 0;
  19274. int dummy_select_end = 0;
  19275. NK_ASSERT(ctx);
  19276. NK_ASSERT(ctx->current);
  19277. NK_ASSERT(ctx->current->layout);
  19278. if (!ctx || !ctx->current || !ctx->current->layout)
  19279. return;
  19280. win = ctx->current;
  19281. layout = win->layout;
  19282. style = &ctx->style;
  19283. s = nk_widget(&bounds, ctx);
  19284. if (!s) return;
  19285. /* calculate hash from name */
  19286. if (name[0] == '#') {
  19287. hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
  19288. name++; /* special number hash */
  19289. } else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
  19290. /* check if property is currently hot item */
  19291. if (win->property.active && hash == win->property.name) {
  19292. buffer = win->property.buffer;
  19293. len = &win->property.length;
  19294. cursor = &win->property.cursor;
  19295. state = &win->property.state;
  19296. select_begin = &win->property.select_start;
  19297. select_end = &win->property.select_end;
  19298. } else {
  19299. buffer = dummy_buffer;
  19300. len = &dummy_length;
  19301. cursor = &dummy_cursor;
  19302. state = &dummy_state;
  19303. select_begin = &dummy_select_begin;
  19304. select_end = &dummy_select_end;
  19305. }
  19306. /* execute property widget */
  19307. old_state = *state;
  19308. ctx->text_edit.clip = ctx->clip;
  19309. in = ((s == NK_WIDGET_ROM && !win->property.active) ||
  19310. layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19311. nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
  19312. variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
  19313. select_end, &style->property, filter, in, style->font, &ctx->text_edit,
  19314. ctx->button_behavior);
  19315. if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
  19316. /* current property is now hot */
  19317. win->property.active = 1;
  19318. NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
  19319. win->property.length = *len;
  19320. win->property.cursor = *cursor;
  19321. win->property.state = *state;
  19322. win->property.name = hash;
  19323. win->property.select_start = *select_begin;
  19324. win->property.select_end = *select_end;
  19325. if (*state == NK_PROPERTY_DRAG) {
  19326. ctx->input.mouse.grab = nk_true;
  19327. ctx->input.mouse.grabbed = nk_true;
  19328. }
  19329. }
  19330. /* check if previously active property is now inactive */
  19331. if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
  19332. if (old_state == NK_PROPERTY_DRAG) {
  19333. ctx->input.mouse.grab = nk_false;
  19334. ctx->input.mouse.grabbed = nk_false;
  19335. ctx->input.mouse.ungrab = nk_true;
  19336. }
  19337. win->property.select_start = 0;
  19338. win->property.select_end = 0;
  19339. win->property.active = 0;
  19340. }
  19341. }
  19342. NK_API void
  19343. nk_property_int(struct nk_context *ctx, const char *name,
  19344. int min, int *val, int max, int step, float inc_per_pixel)
  19345. {
  19346. struct nk_property_variant variant;
  19347. NK_ASSERT(ctx);
  19348. NK_ASSERT(name);
  19349. NK_ASSERT(val);
  19350. if (!ctx || !ctx->current || !name || !val) return;
  19351. variant = nk_property_variant_int(*val, min, max, step);
  19352. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  19353. *val = variant.value.i;
  19354. }
  19355. NK_API void
  19356. nk_property_float(struct nk_context *ctx, const char *name,
  19357. float min, float *val, float max, float step, float inc_per_pixel)
  19358. {
  19359. struct nk_property_variant variant;
  19360. NK_ASSERT(ctx);
  19361. NK_ASSERT(name);
  19362. NK_ASSERT(val);
  19363. if (!ctx || !ctx->current || !name || !val) return;
  19364. variant = nk_property_variant_float(*val, min, max, step);
  19365. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  19366. *val = variant.value.f;
  19367. }
  19368. NK_API void
  19369. nk_property_double(struct nk_context *ctx, const char *name,
  19370. double min, double *val, double max, double step, float inc_per_pixel)
  19371. {
  19372. struct nk_property_variant variant;
  19373. NK_ASSERT(ctx);
  19374. NK_ASSERT(name);
  19375. NK_ASSERT(val);
  19376. if (!ctx || !ctx->current || !name || !val) return;
  19377. variant = nk_property_variant_double(*val, min, max, step);
  19378. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  19379. *val = variant.value.d;
  19380. }
  19381. NK_API int
  19382. nk_propertyi(struct nk_context *ctx, const char *name, int min, int val,
  19383. int max, int step, float inc_per_pixel)
  19384. {
  19385. struct nk_property_variant variant;
  19386. NK_ASSERT(ctx);
  19387. NK_ASSERT(name);
  19388. if (!ctx || !ctx->current || !name) return val;
  19389. variant = nk_property_variant_int(val, min, max, step);
  19390. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  19391. val = variant.value.i;
  19392. return val;
  19393. }
  19394. NK_API float
  19395. nk_propertyf(struct nk_context *ctx, const char *name, float min,
  19396. float val, float max, float step, float inc_per_pixel)
  19397. {
  19398. struct nk_property_variant variant;
  19399. NK_ASSERT(ctx);
  19400. NK_ASSERT(name);
  19401. if (!ctx || !ctx->current || !name) return val;
  19402. variant = nk_property_variant_float(val, min, max, step);
  19403. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  19404. val = variant.value.f;
  19405. return val;
  19406. }
  19407. NK_API double
  19408. nk_propertyd(struct nk_context *ctx, const char *name, double min,
  19409. double val, double max, double step, float inc_per_pixel)
  19410. {
  19411. struct nk_property_variant variant;
  19412. NK_ASSERT(ctx);
  19413. NK_ASSERT(name);
  19414. if (!ctx || !ctx->current || !name) return val;
  19415. variant = nk_property_variant_double(val, min, max, step);
  19416. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  19417. val = variant.value.d;
  19418. return val;
  19419. }
  19420. /*----------------------------------------------------------------
  19421. *
  19422. * COLOR PICKER
  19423. *
  19424. * --------------------------------------------------------------*/
  19425. NK_API int
  19426. nk_color_pick(struct nk_context * ctx, struct nk_color *color,
  19427. enum nk_color_format fmt)
  19428. {
  19429. struct nk_window *win;
  19430. struct nk_panel *layout;
  19431. const struct nk_style *config;
  19432. const struct nk_input *in;
  19433. enum nk_widget_layout_states state;
  19434. struct nk_rect bounds;
  19435. NK_ASSERT(ctx);
  19436. NK_ASSERT(color);
  19437. NK_ASSERT(ctx->current);
  19438. NK_ASSERT(ctx->current->layout);
  19439. if (!ctx || !ctx->current || !ctx->current->layout || !color)
  19440. return 0;
  19441. win = ctx->current;
  19442. config = &ctx->style;
  19443. layout = win->layout;
  19444. state = nk_widget(&bounds, ctx);
  19445. if (!state) return 0;
  19446. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19447. return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
  19448. nk_vec2(0,0), in, config->font);
  19449. }
  19450. NK_API struct nk_color
  19451. nk_color_picker(struct nk_context *ctx, struct nk_color color,
  19452. enum nk_color_format fmt)
  19453. {
  19454. nk_color_pick(ctx, &color, fmt);
  19455. return color;
  19456. }
  19457. /* -------------------------------------------------------------
  19458. *
  19459. * CHART
  19460. *
  19461. * --------------------------------------------------------------*/
  19462. NK_API int
  19463. nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
  19464. struct nk_color color, struct nk_color highlight,
  19465. int count, float min_value, float max_value)
  19466. {
  19467. struct nk_window *win;
  19468. struct nk_chart *chart;
  19469. const struct nk_style *config;
  19470. const struct nk_style_chart *style;
  19471. const struct nk_style_item *background;
  19472. struct nk_rect bounds = {0, 0, 0, 0};
  19473. NK_ASSERT(ctx);
  19474. NK_ASSERT(ctx->current);
  19475. NK_ASSERT(ctx->current->layout);
  19476. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  19477. if (!nk_widget(&bounds, ctx)) {
  19478. chart = &ctx->current->layout->chart;
  19479. nk_zero(chart, sizeof(*chart));
  19480. return 0;
  19481. }
  19482. win = ctx->current;
  19483. config = &ctx->style;
  19484. chart = &win->layout->chart;
  19485. style = &config->chart;
  19486. /* setup basic generic chart */
  19487. nk_zero(chart, sizeof(*chart));
  19488. chart->x = bounds.x + style->padding.x;
  19489. chart->y = bounds.y + style->padding.y;
  19490. chart->w = bounds.w - 2 * style->padding.x;
  19491. chart->h = bounds.h - 2 * style->padding.y;
  19492. chart->w = NK_MAX(chart->w, 2 * style->padding.x);
  19493. chart->h = NK_MAX(chart->h, 2 * style->padding.y);
  19494. /* add first slot into chart */
  19495. {struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  19496. slot->type = type;
  19497. slot->count = count;
  19498. slot->color = color;
  19499. slot->highlight = highlight;
  19500. slot->min = NK_MIN(min_value, max_value);
  19501. slot->max = NK_MAX(min_value, max_value);
  19502. slot->range = slot->max - slot->min;}
  19503. /* draw chart background */
  19504. background = &style->background;
  19505. if (background->type == NK_STYLE_ITEM_IMAGE) {
  19506. nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
  19507. } else {
  19508. nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
  19509. nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
  19510. style->rounding, style->background.data.color);
  19511. }
  19512. return 1;
  19513. }
  19514. NK_API int
  19515. nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type,
  19516. int count, float min_value, float max_value)
  19517. {return nk_chart_begin_colored(ctx, type, ctx->style.chart.color, ctx->style.chart.selected_color, count, min_value, max_value);}
  19518. NK_API void
  19519. nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
  19520. struct nk_color color, struct nk_color highlight,
  19521. int count, float min_value, float max_value)
  19522. {
  19523. NK_ASSERT(ctx);
  19524. NK_ASSERT(ctx->current);
  19525. NK_ASSERT(ctx->current->layout);
  19526. NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
  19527. if (!ctx || !ctx->current || !ctx->current->layout) return;
  19528. if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
  19529. /* add another slot into the graph */
  19530. {struct nk_chart *chart = &ctx->current->layout->chart;
  19531. struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  19532. slot->type = type;
  19533. slot->count = count;
  19534. slot->color = color;
  19535. slot->highlight = highlight;
  19536. slot->min = NK_MIN(min_value, max_value);
  19537. slot->max = NK_MAX(min_value, max_value);
  19538. slot->range = slot->max - slot->min;}
  19539. }
  19540. NK_API void
  19541. nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
  19542. int count, float min_value, float max_value)
  19543. {nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color, ctx->style.chart.selected_color, count, min_value, max_value);}
  19544. NK_INTERN nk_flags
  19545. nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
  19546. struct nk_chart *g, float value, int slot)
  19547. {
  19548. struct nk_panel *layout = win->layout;
  19549. const struct nk_input *i = &ctx->input;
  19550. struct nk_command_buffer *out = &win->buffer;
  19551. nk_flags ret = 0;
  19552. struct nk_vec2 cur;
  19553. struct nk_rect bounds;
  19554. struct nk_color color;
  19555. float step;
  19556. float range;
  19557. float ratio;
  19558. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  19559. step = g->w / (float)g->slots[slot].count;
  19560. range = g->slots[slot].max - g->slots[slot].min;
  19561. ratio = (value - g->slots[slot].min) / range;
  19562. if (g->slots[slot].index == 0) {
  19563. /* first data point does not have a connection */
  19564. g->slots[slot].last.x = g->x;
  19565. g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
  19566. bounds.x = g->slots[slot].last.x - 2;
  19567. bounds.y = g->slots[slot].last.y - 2;
  19568. bounds.w = bounds.h = 4;
  19569. color = g->slots[slot].color;
  19570. if (!(layout->flags & NK_WINDOW_ROM) &&
  19571. NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
  19572. ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
  19573. ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
  19574. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  19575. color = g->slots[slot].highlight;
  19576. }
  19577. nk_fill_rect(out, bounds, 0, color);
  19578. g->slots[slot].index += 1;
  19579. return ret;
  19580. }
  19581. /* draw a line between the last data point and the new one */
  19582. color = g->slots[slot].color;
  19583. cur.x = g->x + (float)(step * (float)g->slots[slot].index);
  19584. cur.y = (g->y + g->h) - (ratio * (float)g->h);
  19585. nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
  19586. bounds.x = cur.x - 3;
  19587. bounds.y = cur.y - 3;
  19588. bounds.w = bounds.h = 6;
  19589. /* user selection of current data point */
  19590. if (!(layout->flags & NK_WINDOW_ROM)) {
  19591. if (nk_input_is_mouse_hovering_rect(i, bounds)) {
  19592. ret = NK_CHART_HOVERING;
  19593. ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
  19594. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  19595. color = g->slots[slot].highlight;
  19596. }
  19597. }
  19598. nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
  19599. /* save current data point position */
  19600. g->slots[slot].last.x = cur.x;
  19601. g->slots[slot].last.y = cur.y;
  19602. g->slots[slot].index += 1;
  19603. return ret;
  19604. }
  19605. NK_INTERN nk_flags
  19606. nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
  19607. struct nk_chart *chart, float value, int slot)
  19608. {
  19609. struct nk_command_buffer *out = &win->buffer;
  19610. const struct nk_input *in = &ctx->input;
  19611. struct nk_panel *layout = win->layout;
  19612. float ratio;
  19613. nk_flags ret = 0;
  19614. struct nk_color color;
  19615. struct nk_rect item = {0,0,0,0};
  19616. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  19617. if (chart->slots[slot].index >= chart->slots[slot].count)
  19618. return nk_false;
  19619. if (chart->slots[slot].count) {
  19620. float padding = (float)(chart->slots[slot].count-1);
  19621. item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
  19622. }
  19623. /* calculate bounds of current bar chart entry */
  19624. color = chart->slots[slot].color;;
  19625. item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
  19626. if (value >= 0) {
  19627. ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
  19628. item.y = (chart->y + chart->h) - chart->h * ratio;
  19629. } else {
  19630. ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
  19631. item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
  19632. }
  19633. item.x = chart->x + ((float)chart->slots[slot].index * item.w);
  19634. item.x = item.x + ((float)chart->slots[slot].index);
  19635. /* user chart bar selection */
  19636. if (!(layout->flags & NK_WINDOW_ROM) &&
  19637. NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
  19638. ret = NK_CHART_HOVERING;
  19639. ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
  19640. in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  19641. color = chart->slots[slot].highlight;
  19642. }
  19643. nk_fill_rect(out, item, 0, color);
  19644. chart->slots[slot].index += 1;
  19645. return ret;
  19646. }
  19647. NK_API nk_flags
  19648. nk_chart_push_slot(struct nk_context *ctx, float value, int slot)
  19649. {
  19650. nk_flags flags;
  19651. struct nk_window *win;
  19652. NK_ASSERT(ctx);
  19653. NK_ASSERT(ctx->current);
  19654. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  19655. NK_ASSERT(slot < ctx->current->layout->chart.slot);
  19656. if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
  19657. if (slot >= ctx->current->layout->chart.slot) return nk_false;
  19658. win = ctx->current;
  19659. if (win->layout->chart.slot < slot) return nk_false;
  19660. switch (win->layout->chart.slots[slot].type) {
  19661. case NK_CHART_LINES:
  19662. flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
  19663. case NK_CHART_COLUMN:
  19664. flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
  19665. default:
  19666. case NK_CHART_MAX:
  19667. flags = 0;
  19668. }
  19669. return flags;
  19670. }
  19671. NK_API nk_flags
  19672. nk_chart_push(struct nk_context *ctx, float value)
  19673. {return nk_chart_push_slot(ctx, value, 0);}
  19674. NK_API void
  19675. nk_chart_end(struct nk_context *ctx)
  19676. {
  19677. struct nk_window *win;
  19678. struct nk_chart *chart;
  19679. NK_ASSERT(ctx);
  19680. NK_ASSERT(ctx->current);
  19681. if (!ctx || !ctx->current)
  19682. return;
  19683. win = ctx->current;
  19684. chart = &win->layout->chart;
  19685. NK_MEMSET(chart, 0, sizeof(*chart));
  19686. return;
  19687. }
  19688. NK_API void
  19689. nk_plot(struct nk_context *ctx, enum nk_chart_type type, const float *values,
  19690. int count, int offset)
  19691. {
  19692. int i = 0;
  19693. float min_value;
  19694. float max_value;
  19695. NK_ASSERT(ctx);
  19696. NK_ASSERT(values);
  19697. if (!ctx || !values || !count) return;
  19698. min_value = values[offset];
  19699. max_value = values[offset];
  19700. for (i = 0; i < count; ++i) {
  19701. min_value = NK_MIN(values[i + offset], min_value);
  19702. max_value = NK_MAX(values[i + offset], max_value);
  19703. }
  19704. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  19705. for (i = 0; i < count; ++i)
  19706. nk_chart_push(ctx, values[i + offset]);
  19707. nk_chart_end(ctx);
  19708. }
  19709. }
  19710. NK_API void
  19711. nk_plot_function(struct nk_context *ctx, enum nk_chart_type type, void *userdata,
  19712. float(*value_getter)(void* user, int index), int count, int offset)
  19713. {
  19714. int i = 0;
  19715. float min_value;
  19716. float max_value;
  19717. NK_ASSERT(ctx);
  19718. NK_ASSERT(value_getter);
  19719. if (!ctx || !value_getter || !count) return;
  19720. max_value = min_value = value_getter(userdata, offset);
  19721. for (i = 0; i < count; ++i) {
  19722. float value = value_getter(userdata, i + offset);
  19723. min_value = NK_MIN(value, min_value);
  19724. max_value = NK_MAX(value, max_value);
  19725. }
  19726. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  19727. for (i = 0; i < count; ++i)
  19728. nk_chart_push(ctx, value_getter(userdata, i + offset));
  19729. nk_chart_end(ctx);
  19730. }
  19731. }
  19732. /* -------------------------------------------------------------
  19733. *
  19734. * GROUP
  19735. *
  19736. * --------------------------------------------------------------*/
  19737. NK_API int
  19738. nk_group_scrolled_offset_begin(struct nk_context *ctx,
  19739. nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags)
  19740. {
  19741. struct nk_rect bounds;
  19742. struct nk_window panel;
  19743. struct nk_window *win;
  19744. win = ctx->current;
  19745. nk_panel_alloc_space(&bounds, ctx);
  19746. {const struct nk_rect *c = &win->layout->clip;
  19747. if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
  19748. !(flags & NK_WINDOW_MOVABLE)) {
  19749. return 0;
  19750. }}
  19751. if (win->flags & NK_WINDOW_ROM)
  19752. flags |= NK_WINDOW_ROM;
  19753. /* initialize a fake window to create the panel from */
  19754. nk_zero(&panel, sizeof(panel));
  19755. panel.bounds = bounds;
  19756. panel.flags = flags;
  19757. panel.scrollbar.x = *x_offset;
  19758. panel.scrollbar.y = *y_offset;
  19759. panel.buffer = win->buffer;
  19760. panel.layout = (struct nk_panel*)nk_create_panel(ctx);
  19761. ctx->current = &panel;
  19762. nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title: 0, NK_PANEL_GROUP);
  19763. win->buffer = panel.buffer;
  19764. win->buffer.clip = panel.layout->clip;
  19765. panel.layout->offset_x = x_offset;
  19766. panel.layout->offset_y = y_offset;
  19767. panel.layout->parent = win->layout;
  19768. win->layout = panel.layout;
  19769. ctx->current = win;
  19770. if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
  19771. (panel.layout->flags & NK_WINDOW_MINIMIZED))
  19772. {
  19773. nk_flags f = panel.layout->flags;
  19774. nk_group_scrolled_end(ctx);
  19775. if (f & NK_WINDOW_CLOSED)
  19776. return NK_WINDOW_CLOSED;
  19777. if (f & NK_WINDOW_MINIMIZED)
  19778. return NK_WINDOW_MINIMIZED;
  19779. }
  19780. return 1;
  19781. }
  19782. NK_API void
  19783. nk_group_scrolled_end(struct nk_context *ctx)
  19784. {
  19785. struct nk_window *win;
  19786. struct nk_panel *parent;
  19787. struct nk_panel *g;
  19788. struct nk_rect clip;
  19789. struct nk_window pan;
  19790. struct nk_vec2 panel_padding;
  19791. NK_ASSERT(ctx);
  19792. NK_ASSERT(ctx->current);
  19793. if (!ctx || !ctx->current)
  19794. return;
  19795. /* make sure nk_group_begin was called correctly */
  19796. NK_ASSERT(ctx->current);
  19797. win = ctx->current;
  19798. NK_ASSERT(win->layout);
  19799. g = win->layout;
  19800. NK_ASSERT(g->parent);
  19801. parent = g->parent;
  19802. /* dummy window */
  19803. nk_zero_struct(pan);
  19804. panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
  19805. pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
  19806. pan.bounds.x = g->bounds.x - panel_padding.x;
  19807. pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
  19808. pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
  19809. if (g->flags & NK_WINDOW_BORDER) {
  19810. pan.bounds.x -= g->border;
  19811. pan.bounds.y -= g->border;
  19812. pan.bounds.w += 2*g->border;
  19813. pan.bounds.h += 2*g->border;
  19814. }
  19815. if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
  19816. pan.bounds.w += ctx->style.window.scrollbar_size.x;
  19817. pan.bounds.h += ctx->style.window.scrollbar_size.y;
  19818. }
  19819. pan.scrollbar.x = *g->offset_x;
  19820. pan.scrollbar.y = *g->offset_y;
  19821. pan.flags = g->flags;
  19822. pan.buffer = win->buffer;
  19823. pan.layout = g;
  19824. pan.parent = win;
  19825. ctx->current = &pan;
  19826. /* make sure group has correct clipping rectangle */
  19827. nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
  19828. pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
  19829. nk_push_scissor(&pan.buffer, clip);
  19830. nk_end(ctx);
  19831. win->buffer = pan.buffer;
  19832. nk_push_scissor(&win->buffer, parent->clip);
  19833. ctx->current = win;
  19834. win->layout = parent;
  19835. g->bounds = pan.bounds;
  19836. return;
  19837. }
  19838. NK_API int
  19839. nk_group_scrolled_begin(struct nk_context *ctx,
  19840. struct nk_scroll *scroll, const char *title, nk_flags flags)
  19841. {return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);}
  19842. NK_API int
  19843. nk_group_begin(struct nk_context *ctx, const char *title, nk_flags flags)
  19844. {
  19845. int title_len;
  19846. nk_hash title_hash;
  19847. struct nk_window *win;
  19848. nk_uint *x_offset;
  19849. nk_uint *y_offset;
  19850. NK_ASSERT(ctx);
  19851. NK_ASSERT(title);
  19852. NK_ASSERT(ctx->current);
  19853. NK_ASSERT(ctx->current->layout);
  19854. if (!ctx || !ctx->current || !ctx->current->layout || !title)
  19855. return 0;
  19856. /* find persistent group scrollbar value */
  19857. win = ctx->current;
  19858. title_len = (int)nk_strlen(title);
  19859. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  19860. x_offset = nk_find_value(win, title_hash);
  19861. if (!x_offset) {
  19862. x_offset = nk_add_value(ctx, win, title_hash, 0);
  19863. y_offset = nk_add_value(ctx, win, title_hash+1, 0);
  19864. NK_ASSERT(x_offset);
  19865. NK_ASSERT(y_offset);
  19866. if (!x_offset || !y_offset) return 0;
  19867. *x_offset = *y_offset = 0;
  19868. } else y_offset = nk_find_value(win, title_hash+1);
  19869. return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  19870. }
  19871. NK_API void
  19872. nk_group_end(struct nk_context *ctx)
  19873. {nk_group_scrolled_end(ctx);}
  19874. NK_API int
  19875. nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
  19876. const char *title, nk_flags flags, int row_height, int row_count)
  19877. {
  19878. int title_len;
  19879. nk_hash title_hash;
  19880. nk_uint *x_offset;
  19881. nk_uint *y_offset;
  19882. int result;
  19883. struct nk_window *win;
  19884. struct nk_panel *layout;
  19885. const struct nk_style *style;
  19886. struct nk_vec2 item_spacing;
  19887. NK_ASSERT(ctx);
  19888. NK_ASSERT(view);
  19889. NK_ASSERT(title);
  19890. if (!ctx || !view || !title) return 0;
  19891. win = ctx->current;
  19892. style = &ctx->style;
  19893. item_spacing = style->window.spacing;
  19894. row_height += NK_MAX(0, (int)item_spacing.y);
  19895. /* find persistent list view scrollbar offset */
  19896. title_len = (int)nk_strlen(title);
  19897. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  19898. x_offset = nk_find_value(win, title_hash);
  19899. if (!x_offset) {
  19900. x_offset = nk_add_value(ctx, win, title_hash, 0);
  19901. y_offset = nk_add_value(ctx, win, title_hash+1, 0);
  19902. NK_ASSERT(x_offset);
  19903. NK_ASSERT(y_offset);
  19904. if (!x_offset || !y_offset) return 0;
  19905. *x_offset = *y_offset = 0;
  19906. } else y_offset = nk_find_value(win, title_hash+1);
  19907. view->scroll_value = *y_offset;
  19908. view->scroll_pointer = y_offset;
  19909. *y_offset = 0;
  19910. result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  19911. win = ctx->current;
  19912. layout = win->layout;
  19913. view->total_height = row_height * NK_MAX(row_count,1);
  19914. view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
  19915. view->count = (int)NK_MAX(nk_iceilf((layout->clip.h)/(float)row_height), 0);
  19916. view->end = view->begin + view->count;
  19917. view->ctx = ctx;
  19918. return result;
  19919. }
  19920. NK_API void
  19921. nk_list_view_end(struct nk_list_view *view)
  19922. {
  19923. struct nk_context *ctx;
  19924. struct nk_window *win;
  19925. struct nk_panel *layout;
  19926. NK_ASSERT(view);
  19927. NK_ASSERT(view->ctx);
  19928. NK_ASSERT(view->scroll_pointer);
  19929. if (!view || !view->ctx) return;
  19930. ctx = view->ctx;
  19931. win = ctx->current;
  19932. layout = win->layout;
  19933. layout->at_y = layout->bounds.y + (float)view->total_height;
  19934. *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
  19935. nk_group_end(view->ctx);
  19936. }
  19937. /* --------------------------------------------------------------
  19938. *
  19939. * POPUP
  19940. *
  19941. * --------------------------------------------------------------*/
  19942. NK_API int
  19943. nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
  19944. const char *title, nk_flags flags, struct nk_rect rect)
  19945. {
  19946. struct nk_window *popup;
  19947. struct nk_window *win;
  19948. struct nk_panel *panel;
  19949. int title_len;
  19950. nk_hash title_hash;
  19951. nk_size allocated;
  19952. NK_ASSERT(ctx);
  19953. NK_ASSERT(title);
  19954. NK_ASSERT(ctx->current);
  19955. NK_ASSERT(ctx->current->layout);
  19956. if (!ctx || !ctx->current || !ctx->current->layout)
  19957. return 0;
  19958. win = ctx->current;
  19959. panel = win->layout;
  19960. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
  19961. (void)panel;
  19962. title_len = (int)nk_strlen(title);
  19963. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
  19964. popup = win->popup.win;
  19965. if (!popup) {
  19966. popup = (struct nk_window*)nk_create_window(ctx);
  19967. popup->parent = win;
  19968. win->popup.win = popup;
  19969. win->popup.active = 0;
  19970. win->popup.type = NK_PANEL_POPUP;
  19971. }
  19972. /* make sure we have correct popup */
  19973. if (win->popup.name != title_hash) {
  19974. if (!win->popup.active) {
  19975. nk_zero(popup, sizeof(*popup));
  19976. win->popup.name = title_hash;
  19977. win->popup.active = 1;
  19978. win->popup.type = NK_PANEL_POPUP;
  19979. } else return 0;
  19980. }
  19981. /* popup position is local to window */
  19982. ctx->current = popup;
  19983. rect.x += win->layout->clip.x;
  19984. rect.y += win->layout->clip.y;
  19985. /* setup popup data */
  19986. popup->parent = win;
  19987. popup->bounds = rect;
  19988. popup->seq = ctx->seq;
  19989. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  19990. popup->flags = flags;
  19991. popup->flags |= NK_WINDOW_BORDER;
  19992. if (type == NK_POPUP_DYNAMIC)
  19993. popup->flags |= NK_WINDOW_DYNAMIC;
  19994. popup->buffer = win->buffer;
  19995. nk_start_popup(ctx, win);
  19996. allocated = ctx->memory.allocated;
  19997. nk_push_scissor(&popup->buffer, nk_null_rect);
  19998. if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
  19999. /* popup is running therefore invalidate parent panels */
  20000. struct nk_panel *root;
  20001. root = win->layout;
  20002. while (root) {
  20003. root->flags |= NK_WINDOW_ROM;
  20004. root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  20005. root = root->parent;
  20006. }
  20007. win->popup.active = 1;
  20008. popup->layout->offset_x = &popup->scrollbar.x;
  20009. popup->layout->offset_y = &popup->scrollbar.y;
  20010. popup->layout->parent = win->layout;
  20011. return 1;
  20012. } else {
  20013. /* popup was closed/is invalid so cleanup */
  20014. struct nk_panel *root;
  20015. root = win->layout;
  20016. while (root) {
  20017. root->flags |= NK_WINDOW_REMOVE_ROM;
  20018. root = root->parent;
  20019. }
  20020. win->popup.buf.active = 0;
  20021. win->popup.active = 0;
  20022. ctx->memory.allocated = allocated;
  20023. ctx->current = win;
  20024. nk_free_panel(ctx, popup->layout);
  20025. popup->layout = 0;
  20026. return 0;
  20027. }
  20028. }
  20029. NK_INTERN int
  20030. nk_nonblock_begin(struct nk_context *ctx,
  20031. nk_flags flags, struct nk_rect body, struct nk_rect header,
  20032. enum nk_panel_type panel_type)
  20033. {
  20034. struct nk_window *popup;
  20035. struct nk_window *win;
  20036. struct nk_panel *panel;
  20037. int is_active = nk_true;
  20038. NK_ASSERT(ctx);
  20039. NK_ASSERT(ctx->current);
  20040. NK_ASSERT(ctx->current->layout);
  20041. if (!ctx || !ctx->current || !ctx->current->layout)
  20042. return 0;
  20043. /* popups cannot have popups */
  20044. win = ctx->current;
  20045. panel = win->layout;
  20046. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
  20047. (void)panel;
  20048. popup = win->popup.win;
  20049. if (!popup) {
  20050. /* create window for nonblocking popup */
  20051. popup = (struct nk_window*)nk_create_window(ctx);
  20052. popup->parent = win;
  20053. win->popup.win = popup;
  20054. win->popup.type = panel_type;
  20055. nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
  20056. } else {
  20057. /* close the popup if user pressed outside or in the header */
  20058. int pressed, in_body, in_header;
  20059. pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  20060. in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  20061. in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
  20062. if (pressed && (!in_body || in_header))
  20063. is_active = nk_false;
  20064. }
  20065. win->popup.header = header;
  20066. if (!is_active) {
  20067. /* remove read only mode from all parent panels */
  20068. struct nk_panel *root = win->layout;
  20069. while (root) {
  20070. root->flags |= NK_WINDOW_REMOVE_ROM;
  20071. root = root->parent;
  20072. }
  20073. return is_active;
  20074. }
  20075. popup->bounds = body;
  20076. popup->parent = win;
  20077. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  20078. popup->flags = flags;
  20079. popup->flags |= NK_WINDOW_BORDER;
  20080. popup->flags |= NK_WINDOW_DYNAMIC;
  20081. popup->seq = ctx->seq;
  20082. win->popup.active = 1;
  20083. NK_ASSERT(popup->layout);
  20084. nk_start_popup(ctx, win);
  20085. popup->buffer = win->buffer;
  20086. nk_push_scissor(&popup->buffer, nk_null_rect);
  20087. ctx->current = popup;
  20088. nk_panel_begin(ctx, 0, panel_type);
  20089. win->buffer = popup->buffer;
  20090. popup->layout->parent = win->layout;
  20091. popup->layout->offset_x = &popup->scrollbar.x;
  20092. popup->layout->offset_y = &popup->scrollbar.y;
  20093. /* set read only mode to all parent panels */
  20094. {struct nk_panel *root;
  20095. root = win->layout;
  20096. while (root) {
  20097. root->flags |= NK_WINDOW_ROM;
  20098. root = root->parent;
  20099. }}
  20100. return is_active;
  20101. }
  20102. NK_API void
  20103. nk_popup_close(struct nk_context *ctx)
  20104. {
  20105. struct nk_window *popup;
  20106. NK_ASSERT(ctx);
  20107. if (!ctx || !ctx->current) return;
  20108. popup = ctx->current;
  20109. NK_ASSERT(popup->parent);
  20110. NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
  20111. popup->flags |= NK_WINDOW_HIDDEN;
  20112. }
  20113. NK_API void
  20114. nk_popup_end(struct nk_context *ctx)
  20115. {
  20116. struct nk_window *win;
  20117. struct nk_window *popup;
  20118. NK_ASSERT(ctx);
  20119. NK_ASSERT(ctx->current);
  20120. NK_ASSERT(ctx->current->layout);
  20121. if (!ctx || !ctx->current || !ctx->current->layout)
  20122. return;
  20123. popup = ctx->current;
  20124. if (!popup->parent) return;
  20125. win = popup->parent;
  20126. if (popup->flags & NK_WINDOW_HIDDEN) {
  20127. struct nk_panel *root;
  20128. root = win->layout;
  20129. while (root) {
  20130. root->flags |= NK_WINDOW_REMOVE_ROM;
  20131. root = root->parent;
  20132. }
  20133. win->popup.active = 0;
  20134. }
  20135. nk_push_scissor(&popup->buffer, nk_null_rect);
  20136. nk_end(ctx);
  20137. win->buffer = popup->buffer;
  20138. nk_finish_popup(ctx, win);
  20139. ctx->current = win;
  20140. nk_push_scissor(&win->buffer, win->layout->clip);
  20141. }
  20142. /* -------------------------------------------------------------
  20143. *
  20144. * TOOLTIP
  20145. *
  20146. * -------------------------------------------------------------- */
  20147. NK_API int
  20148. nk_tooltip_begin(struct nk_context *ctx, float width)
  20149. {
  20150. struct nk_window *win;
  20151. const struct nk_input *in;
  20152. struct nk_rect bounds;
  20153. int ret;
  20154. NK_ASSERT(ctx);
  20155. NK_ASSERT(ctx->current);
  20156. NK_ASSERT(ctx->current->layout);
  20157. if (!ctx || !ctx->current || !ctx->current->layout)
  20158. return 0;
  20159. /* make sure that no nonblocking popup is currently active */
  20160. win = ctx->current;
  20161. in = &ctx->input;
  20162. if (win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK))
  20163. return 0;
  20164. bounds.w = width;
  20165. bounds.h = nk_null_rect.h;
  20166. bounds.x = (in->mouse.pos.x + 1) - win->layout->clip.x;
  20167. bounds.y = (in->mouse.pos.y + 1) - win->layout->clip.y;
  20168. ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
  20169. "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
  20170. if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  20171. win->popup.type = NK_PANEL_TOOLTIP;
  20172. ctx->current->layout->type = NK_PANEL_TOOLTIP;
  20173. return ret;
  20174. }
  20175. NK_API void
  20176. nk_tooltip_end(struct nk_context *ctx)
  20177. {
  20178. NK_ASSERT(ctx);
  20179. NK_ASSERT(ctx->current);
  20180. if (!ctx || !ctx->current) return;
  20181. ctx->current->seq--;
  20182. nk_popup_close(ctx);
  20183. nk_popup_end(ctx);
  20184. }
  20185. NK_API void
  20186. nk_tooltip(struct nk_context *ctx, const char *text)
  20187. {
  20188. const struct nk_style *style;
  20189. struct nk_vec2 padding;
  20190. int text_len;
  20191. float text_width;
  20192. float text_height;
  20193. NK_ASSERT(ctx);
  20194. NK_ASSERT(ctx->current);
  20195. NK_ASSERT(ctx->current->layout);
  20196. NK_ASSERT(text);
  20197. if (!ctx || !ctx->current || !ctx->current->layout || !text)
  20198. return;
  20199. /* fetch configuration data */
  20200. style = &ctx->style;
  20201. padding = style->window.padding;
  20202. /* calculate size of the text and tooltip */
  20203. text_len = nk_strlen(text);
  20204. text_width = style->font->width(style->font->userdata,
  20205. style->font->height, text, text_len);
  20206. text_width += (4 * padding.x);
  20207. text_height = (style->font->height + 2 * padding.y);
  20208. /* execute tooltip and fill with text */
  20209. if (nk_tooltip_begin(ctx, (float)text_width)) {
  20210. nk_layout_row_dynamic(ctx, (float)text_height, 1);
  20211. nk_text(ctx, text, text_len, NK_TEXT_LEFT);
  20212. nk_tooltip_end(ctx);
  20213. }
  20214. }
  20215. /* -------------------------------------------------------------
  20216. *
  20217. * CONTEXTUAL
  20218. *
  20219. * -------------------------------------------------------------- */
  20220. NK_API int
  20221. nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
  20222. struct nk_rect trigger_bounds)
  20223. {
  20224. struct nk_window *win;
  20225. struct nk_window *popup;
  20226. struct nk_rect body;
  20227. NK_STORAGE const struct nk_rect null_rect = {0,0,0,0};
  20228. int is_clicked = 0;
  20229. int is_active = 0;
  20230. int is_open = 0;
  20231. int ret = 0;
  20232. NK_ASSERT(ctx);
  20233. NK_ASSERT(ctx->current);
  20234. NK_ASSERT(ctx->current->layout);
  20235. if (!ctx || !ctx->current || !ctx->current->layout)
  20236. return 0;
  20237. win = ctx->current;
  20238. ++win->popup.con_count;
  20239. /* check if currently active contextual is active */
  20240. popup = win->popup.win;
  20241. is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
  20242. is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
  20243. if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
  20244. return 0;
  20245. if ((is_clicked && is_open && !is_active) || (!is_open && !is_active && !is_clicked))
  20246. return 0;
  20247. /* calculate contextual position on click */
  20248. win->popup.active_con = win->popup.con_count;
  20249. if (is_clicked) {
  20250. body.x = ctx->input.mouse.pos.x;
  20251. body.y = ctx->input.mouse.pos.y;
  20252. } else {
  20253. body.x = popup->bounds.x;
  20254. body.y = popup->bounds.y;
  20255. }
  20256. body.w = size.x;
  20257. body.h = size.y;
  20258. /* start nonblocking contextual popup */
  20259. ret = nk_nonblock_begin(ctx, flags|NK_WINDOW_NO_SCROLLBAR, body,
  20260. null_rect, NK_PANEL_CONTEXTUAL);
  20261. if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
  20262. else {
  20263. win->popup.active_con = 0;
  20264. if (win->popup.win)
  20265. win->popup.win->flags = 0;
  20266. }
  20267. return ret;
  20268. }
  20269. NK_API int
  20270. nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
  20271. nk_flags alignment)
  20272. {
  20273. struct nk_window *win;
  20274. const struct nk_input *in;
  20275. const struct nk_style *style;
  20276. struct nk_rect bounds;
  20277. enum nk_widget_layout_states state;
  20278. NK_ASSERT(ctx);
  20279. NK_ASSERT(ctx->current);
  20280. NK_ASSERT(ctx->current->layout);
  20281. if (!ctx || !ctx->current || !ctx->current->layout)
  20282. return 0;
  20283. win = ctx->current;
  20284. style = &ctx->style;
  20285. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  20286. if (!state) return nk_false;
  20287. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20288. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  20289. text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
  20290. nk_contextual_close(ctx);
  20291. return nk_true;
  20292. }
  20293. return nk_false;
  20294. }
  20295. NK_API int nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  20296. {return nk_contextual_item_text(ctx, label, nk_strlen(label), align);}
  20297. NK_API int
  20298. nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
  20299. const char *text, int len, nk_flags align)
  20300. {
  20301. struct nk_window *win;
  20302. const struct nk_input *in;
  20303. const struct nk_style *style;
  20304. struct nk_rect bounds;
  20305. enum nk_widget_layout_states state;
  20306. NK_ASSERT(ctx);
  20307. NK_ASSERT(ctx->current);
  20308. NK_ASSERT(ctx->current->layout);
  20309. if (!ctx || !ctx->current || !ctx->current->layout)
  20310. return 0;
  20311. win = ctx->current;
  20312. style = &ctx->style;
  20313. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  20314. if (!state) return nk_false;
  20315. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20316. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
  20317. img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
  20318. nk_contextual_close(ctx);
  20319. return nk_true;
  20320. }
  20321. return nk_false;
  20322. }
  20323. NK_API int nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
  20324. const char *label, nk_flags align)
  20325. {return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);}
  20326. NK_API int
  20327. nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  20328. const char *text, int len, nk_flags align)
  20329. {
  20330. struct nk_window *win;
  20331. const struct nk_input *in;
  20332. const struct nk_style *style;
  20333. struct nk_rect bounds;
  20334. enum nk_widget_layout_states state;
  20335. NK_ASSERT(ctx);
  20336. NK_ASSERT(ctx->current);
  20337. NK_ASSERT(ctx->current->layout);
  20338. if (!ctx || !ctx->current || !ctx->current->layout)
  20339. return 0;
  20340. win = ctx->current;
  20341. style = &ctx->style;
  20342. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  20343. if (!state) return nk_false;
  20344. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20345. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  20346. symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  20347. nk_contextual_close(ctx);
  20348. return nk_true;
  20349. }
  20350. return nk_false;
  20351. }
  20352. NK_API int nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  20353. const char *text, nk_flags align)
  20354. {return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);}
  20355. NK_API void
  20356. nk_contextual_close(struct nk_context *ctx)
  20357. {
  20358. NK_ASSERT(ctx);
  20359. NK_ASSERT(ctx->current);
  20360. NK_ASSERT(ctx->current->layout);
  20361. if (!ctx || !ctx->current || !ctx->current->layout) return;
  20362. nk_popup_close(ctx);
  20363. }
  20364. NK_API void
  20365. nk_contextual_end(struct nk_context *ctx)
  20366. {
  20367. struct nk_window *popup;
  20368. struct nk_panel *panel;
  20369. NK_ASSERT(ctx);
  20370. NK_ASSERT(ctx->current);
  20371. if (!ctx || !ctx->current) return;
  20372. popup = ctx->current;
  20373. panel = popup->layout;
  20374. NK_ASSERT(popup->parent);
  20375. NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
  20376. if (panel->flags & NK_WINDOW_DYNAMIC) {
  20377. /* Close behavior
  20378. This is a bit of a hack solution since we do not know before we end our popup
  20379. how big it will be. We therefore do not directly know when a
  20380. click outside the non-blocking popup must close it at that direct frame.
  20381. Instead it will be closed in the next frame.*/
  20382. struct nk_rect body = {0,0,0,0};
  20383. if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
  20384. struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
  20385. body = panel->bounds;
  20386. body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
  20387. body.h = (panel->bounds.y + panel->bounds.h) - body.y;
  20388. }
  20389. {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  20390. int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  20391. if (pressed && in_body)
  20392. popup->flags |= NK_WINDOW_HIDDEN;
  20393. }
  20394. }
  20395. if (popup->flags & NK_WINDOW_HIDDEN)
  20396. popup->seq = 0;
  20397. nk_popup_end(ctx);
  20398. return;
  20399. }
  20400. /* -------------------------------------------------------------
  20401. *
  20402. * COMBO
  20403. *
  20404. * --------------------------------------------------------------*/
  20405. NK_INTERN int
  20406. nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
  20407. struct nk_vec2 size, int is_clicked, struct nk_rect header)
  20408. {
  20409. struct nk_window *popup;
  20410. int is_open = 0;
  20411. int is_active = 0;
  20412. struct nk_rect body;
  20413. nk_hash hash;
  20414. NK_ASSERT(ctx);
  20415. NK_ASSERT(ctx->current);
  20416. NK_ASSERT(ctx->current->layout);
  20417. if (!ctx || !ctx->current || !ctx->current->layout)
  20418. return 0;
  20419. popup = win->popup.win;
  20420. body.x = header.x;
  20421. body.w = size.x;
  20422. body.y = header.y + header.h-ctx->style.window.combo_border;
  20423. body.h = size.y;
  20424. hash = win->popup.combo_count++;
  20425. is_open = (popup) ? nk_true:nk_false;
  20426. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
  20427. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  20428. (!is_open && !is_active && !is_clicked)) return 0;
  20429. if (!nk_nonblock_begin(ctx, 0, body,
  20430. (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
  20431. win->popup.type = NK_PANEL_COMBO;
  20432. win->popup.name = hash;
  20433. return 1;
  20434. }
  20435. NK_API int
  20436. nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
  20437. struct nk_vec2 size)
  20438. {
  20439. const struct nk_input *in;
  20440. struct nk_window *win;
  20441. struct nk_style *style;
  20442. enum nk_widget_layout_states s;
  20443. int is_clicked = nk_false;
  20444. struct nk_rect header;
  20445. const struct nk_style_item *background;
  20446. struct nk_text text;
  20447. NK_ASSERT(ctx);
  20448. NK_ASSERT(selected);
  20449. NK_ASSERT(ctx->current);
  20450. NK_ASSERT(ctx->current->layout);
  20451. if (!ctx || !ctx->current || !ctx->current->layout || !selected)
  20452. return 0;
  20453. win = ctx->current;
  20454. style = &ctx->style;
  20455. s = nk_widget(&header, ctx);
  20456. if (s == NK_WIDGET_INVALID)
  20457. return 0;
  20458. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  20459. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  20460. is_clicked = nk_true;
  20461. /* draw combo box header background and border */
  20462. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  20463. background = &style->combo.active;
  20464. text.text = style->combo.label_active;
  20465. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  20466. background = &style->combo.hover;
  20467. text.text = style->combo.label_hover;
  20468. } else {
  20469. background = &style->combo.normal;
  20470. text.text = style->combo.label_normal;
  20471. }
  20472. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20473. text.background = nk_rgba(0,0,0,0);
  20474. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  20475. } else {
  20476. text.background = background->data.color;
  20477. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  20478. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  20479. }
  20480. {
  20481. /* print currently selected text item */
  20482. struct nk_rect label;
  20483. struct nk_rect button;
  20484. struct nk_rect content;
  20485. enum nk_symbol_type sym;
  20486. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20487. sym = style->combo.sym_hover;
  20488. else if (is_clicked)
  20489. sym = style->combo.sym_active;
  20490. else sym = style->combo.sym_normal;
  20491. /* calculate button */
  20492. button.w = header.h - 2 * style->combo.button_padding.y;
  20493. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  20494. button.y = header.y + style->combo.button_padding.y;
  20495. button.h = button.w;
  20496. content.x = button.x + style->combo.button.padding.x;
  20497. content.y = button.y + style->combo.button.padding.y;
  20498. content.w = button.w - 2 * style->combo.button.padding.x;
  20499. content.h = button.h - 2 * style->combo.button.padding.y;
  20500. /* draw selected label */
  20501. text.padding = nk_vec2(0,0);
  20502. label.x = header.x + style->combo.content_padding.x;
  20503. label.y = header.y + style->combo.content_padding.y;
  20504. label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;;
  20505. label.h = header.h - 2 * style->combo.content_padding.y;
  20506. nk_widget_text(&win->buffer, label, selected, len, &text,
  20507. NK_TEXT_LEFT, ctx->style.font);
  20508. /* draw open/close button */
  20509. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  20510. &ctx->style.combo.button, sym, style->font);
  20511. }
  20512. return nk_combo_begin(ctx, win, size, is_clicked, header);
  20513. }
  20514. NK_API int nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
  20515. {return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);}
  20516. NK_API int
  20517. nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
  20518. {
  20519. struct nk_window *win;
  20520. struct nk_style *style;
  20521. const struct nk_input *in;
  20522. struct nk_rect header;
  20523. int is_clicked = nk_false;
  20524. enum nk_widget_layout_states s;
  20525. const struct nk_style_item *background;
  20526. NK_ASSERT(ctx);
  20527. NK_ASSERT(ctx->current);
  20528. NK_ASSERT(ctx->current->layout);
  20529. if (!ctx || !ctx->current || !ctx->current->layout)
  20530. return 0;
  20531. win = ctx->current;
  20532. style = &ctx->style;
  20533. s = nk_widget(&header, ctx);
  20534. if (s == NK_WIDGET_INVALID)
  20535. return 0;
  20536. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  20537. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  20538. is_clicked = nk_true;
  20539. /* draw combo box header background and border */
  20540. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  20541. background = &style->combo.active;
  20542. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20543. background = &style->combo.hover;
  20544. else background = &style->combo.normal;
  20545. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20546. nk_draw_image(&win->buffer, header, &background->data.image,nk_white);
  20547. } else {
  20548. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  20549. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  20550. }
  20551. {
  20552. struct nk_rect content;
  20553. struct nk_rect button;
  20554. struct nk_rect bounds;
  20555. enum nk_symbol_type sym;
  20556. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20557. sym = style->combo.sym_hover;
  20558. else if (is_clicked)
  20559. sym = style->combo.sym_active;
  20560. else sym = style->combo.sym_normal;
  20561. /* calculate button */
  20562. button.w = header.h - 2 * style->combo.button_padding.y;
  20563. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  20564. button.y = header.y + style->combo.button_padding.y;
  20565. button.h = button.w;
  20566. content.x = button.x + style->combo.button.padding.x;
  20567. content.y = button.y + style->combo.button.padding.y;
  20568. content.w = button.w - 2 * style->combo.button.padding.x;
  20569. content.h = button.h - 2 * style->combo.button.padding.y;
  20570. /* draw color */
  20571. bounds.h = header.h - 4 * style->combo.content_padding.y;
  20572. bounds.y = header.y + 2 * style->combo.content_padding.y;
  20573. bounds.x = header.x + 2 * style->combo.content_padding.x;
  20574. bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
  20575. nk_fill_rect(&win->buffer, bounds, 0, color);
  20576. /* draw open/close button */
  20577. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  20578. &ctx->style.combo.button, sym, style->font);
  20579. }
  20580. return nk_combo_begin(ctx, win, size, is_clicked, header);
  20581. }
  20582. NK_API int
  20583. nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
  20584. {
  20585. struct nk_window *win;
  20586. struct nk_style *style;
  20587. const struct nk_input *in;
  20588. struct nk_rect header;
  20589. int is_clicked = nk_false;
  20590. enum nk_widget_layout_states s;
  20591. const struct nk_style_item *background;
  20592. struct nk_color sym_background;
  20593. struct nk_color symbol_color;
  20594. NK_ASSERT(ctx);
  20595. NK_ASSERT(ctx->current);
  20596. NK_ASSERT(ctx->current->layout);
  20597. if (!ctx || !ctx->current || !ctx->current->layout)
  20598. return 0;
  20599. win = ctx->current;
  20600. style = &ctx->style;
  20601. s = nk_widget(&header, ctx);
  20602. if (s == NK_WIDGET_INVALID)
  20603. return 0;
  20604. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  20605. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  20606. is_clicked = nk_true;
  20607. /* draw combo box header background and border */
  20608. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  20609. background = &style->combo.active;
  20610. symbol_color = style->combo.symbol_active;
  20611. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  20612. background = &style->combo.hover;
  20613. symbol_color = style->combo.symbol_hover;
  20614. } else {
  20615. background = &style->combo.normal;
  20616. symbol_color = style->combo.symbol_hover;
  20617. }
  20618. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20619. sym_background = nk_rgba(0,0,0,0);
  20620. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  20621. } else {
  20622. sym_background = background->data.color;
  20623. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  20624. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  20625. }
  20626. {
  20627. struct nk_rect bounds = {0,0,0,0};
  20628. struct nk_rect content;
  20629. struct nk_rect button;
  20630. enum nk_symbol_type sym;
  20631. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20632. sym = style->combo.sym_hover;
  20633. else if (is_clicked)
  20634. sym = style->combo.sym_active;
  20635. else sym = style->combo.sym_normal;
  20636. /* calculate button */
  20637. button.w = header.h - 2 * style->combo.button_padding.y;
  20638. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  20639. button.y = header.y + style->combo.button_padding.y;
  20640. button.h = button.w;
  20641. content.x = button.x + style->combo.button.padding.x;
  20642. content.y = button.y + style->combo.button.padding.y;
  20643. content.w = button.w - 2 * style->combo.button.padding.x;
  20644. content.h = button.h - 2 * style->combo.button.padding.y;
  20645. /* draw symbol */
  20646. bounds.h = header.h - 2 * style->combo.content_padding.y;
  20647. bounds.y = header.y + style->combo.content_padding.y;
  20648. bounds.x = header.x + style->combo.content_padding.x;
  20649. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  20650. nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
  20651. 1.0f, style->font);
  20652. /* draw open/close button */
  20653. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  20654. &ctx->style.combo.button, sym, style->font);
  20655. }
  20656. return nk_combo_begin(ctx, win, size, is_clicked, header);
  20657. }
  20658. NK_API int
  20659. nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
  20660. enum nk_symbol_type symbol, struct nk_vec2 size)
  20661. {
  20662. struct nk_window *win;
  20663. struct nk_style *style;
  20664. struct nk_input *in;
  20665. struct nk_rect header;
  20666. int is_clicked = nk_false;
  20667. enum nk_widget_layout_states s;
  20668. const struct nk_style_item *background;
  20669. struct nk_color symbol_color;
  20670. struct nk_text text;
  20671. NK_ASSERT(ctx);
  20672. NK_ASSERT(ctx->current);
  20673. NK_ASSERT(ctx->current->layout);
  20674. if (!ctx || !ctx->current || !ctx->current->layout)
  20675. return 0;
  20676. win = ctx->current;
  20677. style = &ctx->style;
  20678. s = nk_widget(&header, ctx);
  20679. if (!s) return 0;
  20680. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  20681. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  20682. is_clicked = nk_true;
  20683. /* draw combo box header background and border */
  20684. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  20685. background = &style->combo.active;
  20686. symbol_color = style->combo.symbol_active;
  20687. text.text = style->combo.label_active;
  20688. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  20689. background = &style->combo.hover;
  20690. symbol_color = style->combo.symbol_hover;
  20691. text.text = style->combo.label_hover;
  20692. } else {
  20693. background = &style->combo.normal;
  20694. symbol_color = style->combo.symbol_normal;
  20695. text.text = style->combo.label_normal;
  20696. }
  20697. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20698. text.background = nk_rgba(0,0,0,0);
  20699. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  20700. } else {
  20701. text.background = background->data.color;
  20702. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  20703. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  20704. }
  20705. {
  20706. struct nk_rect content;
  20707. struct nk_rect button;
  20708. struct nk_rect label;
  20709. struct nk_rect image;
  20710. enum nk_symbol_type sym;
  20711. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20712. sym = style->combo.sym_hover;
  20713. else if (is_clicked)
  20714. sym = style->combo.sym_active;
  20715. else sym = style->combo.sym_normal;
  20716. /* calculate button */
  20717. button.w = header.h - 2 * style->combo.button_padding.y;
  20718. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  20719. button.y = header.y + style->combo.button_padding.y;
  20720. button.h = button.w;
  20721. content.x = button.x + style->combo.button.padding.x;
  20722. content.y = button.y + style->combo.button.padding.y;
  20723. content.w = button.w - 2 * style->combo.button.padding.x;
  20724. content.h = button.h - 2 * style->combo.button.padding.y;
  20725. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  20726. &ctx->style.combo.button, sym, style->font);
  20727. /* draw symbol */
  20728. image.x = header.x + style->combo.content_padding.x;
  20729. image.y = header.y + style->combo.content_padding.y;
  20730. image.h = header.h - 2 * style->combo.content_padding.y;
  20731. image.w = image.h;
  20732. nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
  20733. 1.0f, style->font);
  20734. /* draw label */
  20735. text.padding = nk_vec2(0,0);
  20736. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  20737. label.y = header.y + style->combo.content_padding.y;
  20738. label.w = (button.x - style->combo.content_padding.x) - label.x;
  20739. label.h = header.h - 2 * style->combo.content_padding.y;
  20740. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  20741. }
  20742. return nk_combo_begin(ctx, win, size, is_clicked, header);
  20743. }
  20744. NK_API int
  20745. nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
  20746. {
  20747. struct nk_window *win;
  20748. struct nk_style *style;
  20749. const struct nk_input *in;
  20750. struct nk_rect header;
  20751. int is_clicked = nk_false;
  20752. enum nk_widget_layout_states s;
  20753. const struct nk_style_item *background;
  20754. NK_ASSERT(ctx);
  20755. NK_ASSERT(ctx->current);
  20756. NK_ASSERT(ctx->current->layout);
  20757. if (!ctx || !ctx->current || !ctx->current->layout)
  20758. return 0;
  20759. win = ctx->current;
  20760. style = &ctx->style;
  20761. s = nk_widget(&header, ctx);
  20762. if (s == NK_WIDGET_INVALID)
  20763. return 0;
  20764. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  20765. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  20766. is_clicked = nk_true;
  20767. /* draw combo box header background and border */
  20768. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  20769. background = &style->combo.active;
  20770. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20771. background = &style->combo.hover;
  20772. else background = &style->combo.normal;
  20773. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20774. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  20775. } else {
  20776. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  20777. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  20778. }
  20779. {
  20780. struct nk_rect bounds = {0,0,0,0};
  20781. struct nk_rect content;
  20782. struct nk_rect button;
  20783. enum nk_symbol_type sym;
  20784. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20785. sym = style->combo.sym_hover;
  20786. else if (is_clicked)
  20787. sym = style->combo.sym_active;
  20788. else sym = style->combo.sym_normal;
  20789. /* calculate button */
  20790. button.w = header.h - 2 * style->combo.button_padding.y;
  20791. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  20792. button.y = header.y + style->combo.button_padding.y;
  20793. button.h = button.w;
  20794. content.x = button.x + style->combo.button.padding.x;
  20795. content.y = button.y + style->combo.button.padding.y;
  20796. content.w = button.w - 2 * style->combo.button.padding.x;
  20797. content.h = button.h - 2 * style->combo.button.padding.y;
  20798. /* draw image */
  20799. bounds.h = header.h - 2 * style->combo.content_padding.y;
  20800. bounds.y = header.y + style->combo.content_padding.y;
  20801. bounds.x = header.x + style->combo.content_padding.x;
  20802. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  20803. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  20804. /* draw open/close button */
  20805. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  20806. &ctx->style.combo.button, sym, style->font);
  20807. }
  20808. return nk_combo_begin(ctx, win, size, is_clicked, header);
  20809. }
  20810. NK_API int
  20811. nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
  20812. struct nk_image img, struct nk_vec2 size)
  20813. {
  20814. struct nk_window *win;
  20815. struct nk_style *style;
  20816. struct nk_input *in;
  20817. struct nk_rect header;
  20818. int is_clicked = nk_false;
  20819. enum nk_widget_layout_states s;
  20820. const struct nk_style_item *background;
  20821. struct nk_text text;
  20822. NK_ASSERT(ctx);
  20823. NK_ASSERT(ctx->current);
  20824. NK_ASSERT(ctx->current->layout);
  20825. if (!ctx || !ctx->current || !ctx->current->layout)
  20826. return 0;
  20827. win = ctx->current;
  20828. style = &ctx->style;
  20829. s = nk_widget(&header, ctx);
  20830. if (!s) return 0;
  20831. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  20832. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  20833. is_clicked = nk_true;
  20834. /* draw combo box header background and border */
  20835. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  20836. background = &style->combo.active;
  20837. text.text = style->combo.label_active;
  20838. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  20839. background = &style->combo.hover;
  20840. text.text = style->combo.label_hover;
  20841. } else {
  20842. background = &style->combo.normal;
  20843. text.text = style->combo.label_normal;
  20844. }
  20845. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20846. text.background = nk_rgba(0,0,0,0);
  20847. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  20848. } else {
  20849. text.background = background->data.color;
  20850. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  20851. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  20852. }
  20853. {
  20854. struct nk_rect content;
  20855. struct nk_rect button;
  20856. struct nk_rect label;
  20857. struct nk_rect image;
  20858. enum nk_symbol_type sym;
  20859. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20860. sym = style->combo.sym_hover;
  20861. else if (is_clicked)
  20862. sym = style->combo.sym_active;
  20863. else sym = style->combo.sym_normal;
  20864. /* calculate button */
  20865. button.w = header.h - 2 * style->combo.button_padding.y;
  20866. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  20867. button.y = header.y + style->combo.button_padding.y;
  20868. button.h = button.w;
  20869. content.x = button.x + style->combo.button.padding.x;
  20870. content.y = button.y + style->combo.button.padding.y;
  20871. content.w = button.w - 2 * style->combo.button.padding.x;
  20872. content.h = button.h - 2 * style->combo.button.padding.y;
  20873. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  20874. &ctx->style.combo.button, sym, style->font);
  20875. /* draw image */
  20876. image.x = header.x + style->combo.content_padding.x;
  20877. image.y = header.y + style->combo.content_padding.y;
  20878. image.h = header.h - 2 * style->combo.content_padding.y;
  20879. image.w = image.h;
  20880. nk_draw_image(&win->buffer, image, &img, nk_white);
  20881. /* draw label */
  20882. text.padding = nk_vec2(0,0);
  20883. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  20884. label.y = header.y + style->combo.content_padding.y;
  20885. label.w = (button.x - style->combo.content_padding.x) - label.x;
  20886. label.h = header.h - 2 * style->combo.content_padding.y;
  20887. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  20888. }
  20889. return nk_combo_begin(ctx, win, size, is_clicked, header);
  20890. }
  20891. NK_API int nk_combo_begin_symbol_label(struct nk_context *ctx,
  20892. const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
  20893. {return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);}
  20894. NK_API int nk_combo_begin_image_label(struct nk_context *ctx,
  20895. const char *selected, struct nk_image img, struct nk_vec2 size)
  20896. {return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);}
  20897. NK_API int nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
  20898. {return nk_contextual_item_text(ctx, text, len, align);}
  20899. NK_API int nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  20900. {return nk_contextual_item_label(ctx, label, align);}
  20901. NK_API int nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
  20902. int len, nk_flags alignment)
  20903. {return nk_contextual_item_image_text(ctx, img, text, len, alignment);}
  20904. NK_API int nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
  20905. const char *text, nk_flags alignment)
  20906. {return nk_contextual_item_image_label(ctx, img, text, alignment);}
  20907. NK_API int nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  20908. const char *text, int len, nk_flags alignment)
  20909. {return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);}
  20910. NK_API int nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  20911. const char *label, nk_flags alignment)
  20912. {return nk_contextual_item_symbol_label(ctx, sym, label, alignment);}
  20913. NK_API void nk_combo_end(struct nk_context *ctx)
  20914. {nk_contextual_end(ctx);}
  20915. NK_API void nk_combo_close(struct nk_context *ctx)
  20916. {nk_contextual_close(ctx);}
  20917. NK_API int
  20918. nk_combo(struct nk_context *ctx, const char **items, int count,
  20919. int selected, int item_height, struct nk_vec2 size)
  20920. {
  20921. int i = 0;
  20922. int max_height;
  20923. struct nk_vec2 item_spacing;
  20924. struct nk_vec2 window_padding;
  20925. NK_ASSERT(ctx);
  20926. NK_ASSERT(items);
  20927. NK_ASSERT(ctx->current);
  20928. if (!ctx || !items ||!count)
  20929. return selected;
  20930. item_spacing = ctx->style.window.spacing;
  20931. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  20932. max_height = count * item_height + count * (int)item_spacing.y;
  20933. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  20934. size.y = NK_MIN(size.y, (float)max_height);
  20935. if (nk_combo_begin_label(ctx, items[selected], size)) {
  20936. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  20937. for (i = 0; i < count; ++i) {
  20938. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  20939. selected = i;
  20940. }
  20941. nk_combo_end(ctx);
  20942. }
  20943. return selected;
  20944. }
  20945. NK_API int
  20946. nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  20947. int separator, int selected, int count, int item_height, struct nk_vec2 size)
  20948. {
  20949. int i;
  20950. int max_height;
  20951. struct nk_vec2 item_spacing;
  20952. struct nk_vec2 window_padding;
  20953. const char *current_item;
  20954. const char *iter;
  20955. int length = 0;
  20956. NK_ASSERT(ctx);
  20957. NK_ASSERT(items_separated_by_separator);
  20958. if (!ctx || !items_separated_by_separator)
  20959. return selected;
  20960. /* calculate popup window */
  20961. item_spacing = ctx->style.window.spacing;
  20962. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  20963. max_height = count * item_height + count * (int)item_spacing.y;
  20964. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  20965. size.y = NK_MIN(size.y, (float)max_height);
  20966. /* find selected item */
  20967. current_item = items_separated_by_separator;
  20968. for (i = 0; i < count; ++i) {
  20969. iter = current_item;
  20970. while (*iter && *iter != separator) iter++;
  20971. length = (int)(iter - current_item);
  20972. if (i == selected) break;
  20973. current_item = iter + 1;
  20974. }
  20975. if (nk_combo_begin_text(ctx, current_item, length, size)) {
  20976. current_item = items_separated_by_separator;
  20977. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  20978. for (i = 0; i < count; ++i) {
  20979. iter = current_item;
  20980. while (*iter && *iter != separator) iter++;
  20981. length = (int)(iter - current_item);
  20982. if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
  20983. selected = i;
  20984. current_item = current_item + length + 1;
  20985. }
  20986. nk_combo_end(ctx);
  20987. }
  20988. return selected;
  20989. }
  20990. NK_API int
  20991. nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  20992. int selected, int count, int item_height, struct nk_vec2 size)
  20993. {return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);}
  20994. NK_API int
  20995. nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
  20996. void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
  20997. {
  20998. int i;
  20999. int max_height;
  21000. struct nk_vec2 item_spacing;
  21001. struct nk_vec2 window_padding;
  21002. const char *item;
  21003. NK_ASSERT(ctx);
  21004. NK_ASSERT(item_getter);
  21005. if (!ctx || !item_getter)
  21006. return selected;
  21007. /* calculate popup window */
  21008. item_spacing = ctx->style.window.spacing;
  21009. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  21010. max_height = count * item_height + count * (int)item_spacing.y;
  21011. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  21012. size.y = NK_MIN(size.y, (float)max_height);
  21013. item_getter(userdata, selected, &item);
  21014. if (nk_combo_begin_label(ctx, item, size)) {
  21015. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  21016. for (i = 0; i < count; ++i) {
  21017. item_getter(userdata, i, &item);
  21018. if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
  21019. selected = i;
  21020. }
  21021. nk_combo_end(ctx);
  21022. }
  21023. return selected;
  21024. }
  21025. NK_API void nk_combobox(struct nk_context *ctx, const char **items, int count,
  21026. int *selected, int item_height, struct nk_vec2 size)
  21027. {*selected = nk_combo(ctx, items, count, *selected, item_height, size);}
  21028. NK_API void nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  21029. int *selected, int count, int item_height, struct nk_vec2 size)
  21030. {*selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);}
  21031. NK_API void nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  21032. int separator,int *selected, int count, int item_height, struct nk_vec2 size)
  21033. {*selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
  21034. *selected, count, item_height, size);}
  21035. NK_API void nk_combobox_callback(struct nk_context *ctx,
  21036. void(*item_getter)(void* data, int id, const char **out_text),
  21037. void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
  21038. {*selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);}
  21039. /*
  21040. * -------------------------------------------------------------
  21041. *
  21042. * MENU
  21043. *
  21044. * --------------------------------------------------------------
  21045. */
  21046. NK_INTERN int
  21047. nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
  21048. const char *id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
  21049. {
  21050. int is_open = 0;
  21051. int is_active = 0;
  21052. struct nk_rect body;
  21053. struct nk_window *popup;
  21054. nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
  21055. NK_ASSERT(ctx);
  21056. NK_ASSERT(ctx->current);
  21057. NK_ASSERT(ctx->current->layout);
  21058. if (!ctx || !ctx->current || !ctx->current->layout)
  21059. return 0;
  21060. body.x = header.x;
  21061. body.w = size.x;
  21062. body.y = header.y + header.h;
  21063. body.h = size.y;
  21064. popup = win->popup.win;
  21065. is_open = popup ? nk_true : nk_false;
  21066. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
  21067. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  21068. (!is_open && !is_active && !is_clicked)) return 0;
  21069. if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
  21070. return 0;
  21071. win->popup.type = NK_PANEL_MENU;
  21072. win->popup.name = hash;
  21073. return 1;
  21074. }
  21075. NK_API int
  21076. nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
  21077. nk_flags align, struct nk_vec2 size)
  21078. {
  21079. struct nk_window *win;
  21080. const struct nk_input *in;
  21081. struct nk_rect header;
  21082. int is_clicked = nk_false;
  21083. nk_flags state;
  21084. NK_ASSERT(ctx);
  21085. NK_ASSERT(ctx->current);
  21086. NK_ASSERT(ctx->current->layout);
  21087. if (!ctx || !ctx->current || !ctx->current->layout)
  21088. return 0;
  21089. win = ctx->current;
  21090. state = nk_widget(&header, ctx);
  21091. if (!state) return 0;
  21092. in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21093. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
  21094. title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  21095. is_clicked = nk_true;
  21096. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  21097. }
  21098. NK_API int nk_menu_begin_label(struct nk_context *ctx,
  21099. const char *text, nk_flags align, struct nk_vec2 size)
  21100. {return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);}
  21101. NK_API int
  21102. nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
  21103. struct nk_vec2 size)
  21104. {
  21105. struct nk_window *win;
  21106. struct nk_rect header;
  21107. const struct nk_input *in;
  21108. int is_clicked = nk_false;
  21109. nk_flags state;
  21110. NK_ASSERT(ctx);
  21111. NK_ASSERT(ctx->current);
  21112. NK_ASSERT(ctx->current->layout);
  21113. if (!ctx || !ctx->current || !ctx->current->layout)
  21114. return 0;
  21115. win = ctx->current;
  21116. state = nk_widget(&header, ctx);
  21117. if (!state) return 0;
  21118. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21119. if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
  21120. img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
  21121. is_clicked = nk_true;
  21122. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  21123. }
  21124. NK_API int
  21125. nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
  21126. enum nk_symbol_type sym, struct nk_vec2 size)
  21127. {
  21128. struct nk_window *win;
  21129. const struct nk_input *in;
  21130. struct nk_rect header;
  21131. int is_clicked = nk_false;
  21132. nk_flags state;
  21133. NK_ASSERT(ctx);
  21134. NK_ASSERT(ctx->current);
  21135. NK_ASSERT(ctx->current->layout);
  21136. if (!ctx || !ctx->current || !ctx->current->layout)
  21137. return 0;
  21138. win = ctx->current;
  21139. state = nk_widget(&header, ctx);
  21140. if (!state) return 0;
  21141. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21142. if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
  21143. sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  21144. is_clicked = nk_true;
  21145. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  21146. }
  21147. NK_API int
  21148. nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
  21149. nk_flags align, struct nk_image img, struct nk_vec2 size)
  21150. {
  21151. struct nk_window *win;
  21152. struct nk_rect header;
  21153. const struct nk_input *in;
  21154. int is_clicked = nk_false;
  21155. nk_flags state;
  21156. NK_ASSERT(ctx);
  21157. NK_ASSERT(ctx->current);
  21158. NK_ASSERT(ctx->current->layout);
  21159. if (!ctx || !ctx->current || !ctx->current->layout)
  21160. return 0;
  21161. win = ctx->current;
  21162. state = nk_widget(&header, ctx);
  21163. if (!state) return 0;
  21164. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21165. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  21166. header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  21167. ctx->style.font, in))
  21168. is_clicked = nk_true;
  21169. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  21170. }
  21171. NK_API int nk_menu_begin_image_label(struct nk_context *ctx,
  21172. const char *title, nk_flags align, struct nk_image img, struct nk_vec2 size)
  21173. {return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);}
  21174. NK_API int
  21175. nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
  21176. nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  21177. {
  21178. struct nk_window *win;
  21179. struct nk_rect header;
  21180. const struct nk_input *in;
  21181. int is_clicked = nk_false;
  21182. nk_flags state;
  21183. NK_ASSERT(ctx);
  21184. NK_ASSERT(ctx->current);
  21185. NK_ASSERT(ctx->current->layout);
  21186. if (!ctx || !ctx->current || !ctx->current->layout)
  21187. return 0;
  21188. win = ctx->current;
  21189. state = nk_widget(&header, ctx);
  21190. if (!state) return 0;
  21191. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21192. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
  21193. header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  21194. ctx->style.font, in)) is_clicked = nk_true;
  21195. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  21196. }
  21197. NK_API int nk_menu_begin_symbol_label(struct nk_context *ctx,
  21198. const char *title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size )
  21199. {return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align,sym,size);}
  21200. NK_API int nk_menu_item_text(struct nk_context *ctx, const char *title, int len, nk_flags align)
  21201. {return nk_contextual_item_text(ctx, title, len, align);}
  21202. NK_API int nk_menu_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  21203. {return nk_contextual_item_label(ctx, label, align);}
  21204. NK_API int nk_menu_item_image_label(struct nk_context *ctx, struct nk_image img,
  21205. const char *label, nk_flags align)
  21206. {return nk_contextual_item_image_label(ctx, img, label, align);}
  21207. NK_API int nk_menu_item_image_text(struct nk_context *ctx, struct nk_image img,
  21208. const char *text, int len, nk_flags align)
  21209. {return nk_contextual_item_image_text(ctx, img, text, len, align);}
  21210. NK_API int nk_menu_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  21211. const char *text, int len, nk_flags align)
  21212. {return nk_contextual_item_symbol_text(ctx, sym, text, len, align);}
  21213. NK_API int nk_menu_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  21214. const char *label, nk_flags align)
  21215. {return nk_contextual_item_symbol_label(ctx, sym, label, align);}
  21216. NK_API void nk_menu_close(struct nk_context *ctx)
  21217. {nk_contextual_close(ctx);}
  21218. NK_API void
  21219. nk_menu_end(struct nk_context *ctx)
  21220. {nk_contextual_end(ctx);}
  21221. #endif