Skip to content

MikuAuahDark/NPad93

Repository files navigation

NPad Libraries

Various NPad Lua libraries for LOVE can be found here. Most libs are single files unless noted.

Check each file/folder for license terms.

declarations

This is not a library, but it's type definitions.

  • Files ending with .ts is meant to be used with TypeScriptToLua.
  • Files ending with .tl is meant to be used with Teal.

Each file correspond to each module, unless noted. Note that some module may not have type definitions, yet.

NVec

NPad Vector library, LuaJIT FFI-accelerated hump.vector-compatible vector library. Meant as drop-in replacement of hump.vector. Originally written for my game for optimization purpose, but I think it's better if I make this as standalone and let everyone use it.

For documentation, check out hump.vector documentation: https://hump.readthedocs.io/en/latest/vector.html

NGrading

NPad Color Grading, provides easy color grading for your post-processing needs.

For documentation, check out the ngrading folder.

NWFC

NPad Wave Function Collapse, WFC implemented in Lua and tries to be 1:1 mapping between the original WFC.

NFML

NPad FFI/Fast/Fine Math Library, meant as alternative to CPML. Work in progress!

Functions mostly follows GLSL function names.

NLog

NPad Logging library.

  • Uses ANSI color codes on Linux, macOS, and Windows 10 1607

  • Uses Windows console API on Windows 10 prior 1607

  • Uses Android native logging functions

This library missed iOS implementation and may not run with it.

The log level are divided by 4: info, warn, error, and debug. NLog exports nlog.info, nlog.warn, ... and so on. Furthermore, there's also functions with f suffix (nlog.infof) which accepts formatted string same as string.format.

There's nlog.getLevel to retrieve the current logging level:

  • 0 = don't print anything

  • 1 = print errors (nlog.error/nlog.errorf)

  • 2 = print warnings (nlog.warn/nlog.warnf)

  • 3 = print information (nlog.info/nlog.infof)

  • 4 = print debug information (nlog.debug/nlog.debugf)

Ensure to modify the ENVIRONMENT_VARIABLE variable prior using the library. It's NLOG_LOGLEVEL environment variable by default.

NLay

NPad Layouting Library

NLay (pronounced "Enlay") is layouting library inspired by the flexibility of Android's ConstraintLayout. This layouting library attempts to implement subset of the ConstraintLayout layouting functionality.

NLay is NOT full UI library. It merely function as helper on element placement on the screen. However you are 100% allowed to use NLay for your full featured UI library!

Example:

Example

Code to reproduce rectangle placement above are as follows

local NLay = require("nlay")
local love = require("love")

local function drawRectCenter(text, rect, font)
	local w = font:getWidth(text)
	local h = font:getHeight()
	local root = NLay

	local x, y = NLay.inside(rect):constraint(rect, rect, rect, rect):size(w, h):get()
	love.graphics.print(text, font, x, y)
end

function love.load()
	NLay.update(love.window.getSafeArea())
end

function love.draw()
	local root = NLay
	local insideRoot = NLay.inside(root, 10)

	local rect1 = insideRoot:constraint(root, root)
		:size(100, 100)
	local rect2 = insideRoot:constraint(root, rect1, nil, root)
		:size(0, 100)
		:margin({nil, 10})
	local rect3 = insideRoot:constraint(rect2, rect2, nil, rect2)
		:size(150, 100)
		:margin({10})
	local rect4 = insideRoot:constraint(rect3, rect3)
		:into(false, true)
		:size(75, 100)
		:margin({10})
	local rect5 = insideRoot:constraint(nil, NLay.line(insideRoot, "horizontal", "percent", 0.25), NLay.line(insideRoot, "vertical", "percent", -0.2))
		:size(96, 96)
	local rect6 = insideRoot:constraint(rect2, rect2, rect5, rect4)
		:size(0, 0)
		:into(false, true, false, false)
		:margin({10, 0, 10, 10})
		:ratio(9/16)

	love.graphics.setColor(0.3, 0.3, 0.3)
	love.graphics.rectangle("fill", rect1:get())
	love.graphics.rectangle("fill", rect2:get())
	love.graphics.rectangle("fill", rect3:get())
	love.graphics.rectangle("fill", rect4:get())
	love.graphics.rectangle("fill", rect5:get())
	love.graphics.rectangle("fill", rect6:get())

	local font = love.graphics.getFont()
	love.graphics.setColor(1, 1, 1)
	drawRectCenter("Rectangle 1", rect1, font)
	drawRectCenter("Rectangle 2", rect2, font)
	drawRectCenter("Rectangle 3", rect3, font)
	drawRectCenter("Rectangle 4", rect4, font)
	drawRectCenter("Rectangle 5", rect5, font)
	drawRectCenter("Rectangle 6", rect6, font)
end

function love.resize()
	NLay.update(love.window.getSafeArea())
end

Features not implemented yet:

Documentation can be found at https://github.com/MikuAuahDark/NPad93/blob/master/doc/NLay.md

NAMI

NPad Audio Metadata Inspector

A file-based audio metadata reader for your custom user tracks needs. Work-in-progress!

NAFL

NPad Advanced Frame Limiter

A frame limiter with 3 possible modes that can be switched at runtime.

Full markdown documentation will be written soon. Currently all public functions are annotated.

NTT

NPad Tween Timer

flux-compatible tween implementation with additional enhancements and restrictions:

For documentation and usage, please see flux repository: https://github.com/rxi/flux

If you want to replace existing flux library, either rename ntt.lua to flux.lua or replace all requires from flux to ntt.

NAniTe

NPad Animation Timeline

Timeline-based animation system with support of forward and backward update.

Demo LOVE Project v1.0.0. Control are as follows:

  • space - Pause/resume the animation
  • enter - Toggle between forward or backward update
  • left and right arrow button - Move the animation backward and forward 5ms (/tick with key repeat).
  • shift - Increase the behavior above 3x faster (so 15ms)

Documentation can be found at https://github.com/MikuAuahDark/NPad93/blob/master/doc/NAniTe.md

shlex

Simple lexical analysis, translated directly from Python's shlex.

From Python documentation: The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for example, in run control files for Python applications) or for parsing quoted strings.

Note: This library is untested with Python's test vectors. Use at your own discretion.

vires

Virtual resolution system with safe area support.

Best used with NLay.

Manami

A library to display text as one character at a time. Similar to reflowprint but supports UTF-8, colored text, and justify alignment.

manami_test.mp4

Example

local manami = require("manami")
local textToWrite = {
	{1, 0, 0}, "This text is written ",
	{1, 1, 0}, "to demonstrate the ",
	{0, 1, 0}, "functionality of 愛海 (Manami), ",
	{0, 1, 1}, "an improved reflowprint ",
	{0, 0, 1}, "with UTF-8 and multicolor ",
	{1, 1, 1}, "support along with justify alignment.",
}
local reflow = manami(textToWrite, width, targetAlignment)

The manami function signature is as follows:

manami manami(string|table text, limit, align, font, separator, lengthCalc): manami

Where:

  • text - String or colored text according to LOVE colored text format.
  • limit - Wrap the line after this many horizontal pixels.
  • align - Text alignment. Default is "left".
  • font - Font to use. Default is return value of love.graphics.getFont() when this function is called.
  • separator - How to separate the text out. If this is string, then it's treated as delimiter. If this is function, then it returns how many character to consume. Default is function that always return 1 (consume one character at a time).
  • lengthCalc - How to calculate the duration of each character before showing the next one. By default this is a function that returns the amount of character passed.
  • Returns new manami object.

The function signature of separator (if function is passed) is as follows:

number separator(string s)

Where:

  • s - Unconsumed text.
  • Returns number of how many (UTF-8) character(s) (not bytes) to consume from s.

The function signature of lengthCalc is as follows:

number lengthCalc(string s)

Where:

  • s - The chopped text based on return value of separator function.
  • Returns number of the text duration in time units.

The definition of time units is up to user. If user assume it's in seconds, then with default separator and lengthCalc, the character will be displayed one-by-one every second.

A demo main.lua can be found here: https://gist.github.com/MikuAuahDark/95c5b626b9607da80d9372d906c3d7b6#file-main-lua (don't use the outdated manami.lua there).

NPNN

NPad Neural Network

A simple neural network implementation. It has implenentation of Linear/Dense/FC layer and LSTM layer.

Normally you pass the weights exported from PyTorch to the layers.

import json
import torch

# Say you have PyTorch model in "model" variable
model: torch.nn.Module

class EncodeTensor(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, torch.Tensor):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

with open("modeldata.json", "w", encoding="UTF-8") as f:
    json.dump(model.state_dict(), f, cls=EncodeTensor, ensure_ascii=False)

Then you load modeldata.json using your favorite JSON decoder and pass the weights and the biases to the NN functions.

N9P

NPad93's 9-Patch.

Yet another 9-patch library for LÖVE.

Compared to different implementation, it has an API on constructing your own slices and your own texture or let the library do everything for you using .9.png image.

Simple construction example:

-- Automatic mode. image.9.png must follow Android 9-patch drawable spec.
local n9p = require("n9p")
local stretchableImage = n9p.loadFromImage("image.9.png")

-- stretchableImage is now usable

Advanced construction example:

-- Manual mode. Specify the stretchable regions yourself.
local n9p = require("n9p")
local existingImage = love.graphics.newImage("image_32x32.png")
local stretchableImage = n9p.newBuilder()
	:addHorizontalSlice(8, 24)
	:addVerticalSlice(8, 24)
	:setHorizontalPadding(4, 28)
	:setVerticalPadding(4, 28)
	:build(existingImage:getDimensions())
stretchableImage:setTexture(existingImage)

-- stretchableImage is now usable

Drawing example:

local stretchableImage = ... -- (see above)
local rootWindowGetter = {get = love.window.getSafeArea} -- fulfils get(self) = x,y,w,h

function love.draw()
	-- Method 1 of drawing: specifying position directly
	local w, h = love.graphics.getDimensions()
	stretchableImage:draw(0, 0, w, h)

	-- Method 2 of drawing: specifying constraint object
	stretchableImage:drawConstraint(rootWindowGetter)
end

No separate mrkdown documentation for now, but the public APIs are fully documented.

About

MikuAuahDark's Various Lua Libraries

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages