CabinGame/Luxe/math/util.wren
Ronja 83b933f8a7 more fixes
runs now
2021-02-09 09:44:12 +01:00

127 lines
No EOL
3.5 KiB
Text

import "luxe: assets" for Assets
import "luxe: render" for Material
import "luxe: ui/control" for Control
import "luxe: world" for UIEvent, UI, Entity
import "math/math" for M
import "math/vector" for Vector
import "math/rect" for AABB
class Util{
static material_from_image_path(path){
var image = Assets.image(path)
return material_from_image(image)
}
static material_from_image(image){
var material = Material.create("luxe: material_basis/sprite_pixelated")
Material.set_input(material, "sprite.image", image)
return material
}
static for_all(sequences, fn){
var count = sequences.count
var counter = (0...count)
var iterators = List.filled(count, null)
counter.each{|i| iterators[i] = sequences[i].iterate(iterators[i])}
while(iterators.all{|iter| !!iter}){
var values = (0...count).map{|i| sequences[i].iteratorValue(iterators[i])}.toList
call_arg_list(fn, values)
counter.each{|i| iterators[i] = sequences[i].iterate(iterators[i])}
}
}
static call_arg_list(fn, args){
//cover non-list cases
if(args is Null){
fn.call()
return
}
if(!(args is Sequence)){
fn.call(args)
return
}
//list cases
var l = args.count
if(l == 0){
fn.call()
} else if(l == 1) {
fn.call(args[0])
} else if(l == 2) {
fn.call(args[0], args[1])
} else if(l == 3) {
fn.call(args[0], args[1], args[2])
} else if(l == 4) {
fn.call(args[0], args[1], args[2], args[3])
} else if(l == 5) {
fn.call(args[0], args[1], args[2], args[3], args[4])
} else if(l == 6) {
fn.call(args[0], args[1], args[2], args[3], args[4], args[5])
} else if(l == 7) {
fn.call(args[0], args[1], args[2], args[3], args[4], args[5], args[6])
} else if(l == 8) {
fn.call(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7])
} else if(l == 9) {
fn.call(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8])
} else {
System.print("%(l) is too many arguments")
}
}
static valid_event(event){
return valid_event(event, false)
}
static valid_event(event, allow_cancelled){
//cancelled events are not valid :V
var entity = Control.get_entity(event.control)
if(!allow_cancelled && UI.event_cancelled(entity, event.id)) return false
//events without a position are always valid
if(![UIEvent.press, UIEvent.release, UIEvent.move].contains(event.type)) return true
//when a coltrol has captured the mouse, also everything is valid
if(UI.get_captured(entity)) return true
//if its a event with a position (checked with list) and not captured, the event is only valid when the mouse is over the control
var valid = Control.contains(event.control, event.x, event.y)
return valid
}
//hue value saturation to rgb colors. all values in 0-1 range
static hsv(h, s, v){
//hue to rgb
h = h % 1
var col = [
(h * 6 - 3).abs - 1,
2 - (h * 6 - 2).abs,
2 - (h * 6 - 4).abs
]
col = M.clamp(col, 0, 1)
//then add saturation
col = M.lerp(1, col, s)
//and value
col = col.map{|comp| comp * v}
return col.toList + [1]
}
}
class Actions{
static actions{
if(!__actions) __actions = []
return __actions
}
static queue(fn){
actions.add(fn)
}
static execute(){
actions.each{|action|
action.call()
}
actions.clear()
}
}