import "luxe: ui/control" for Control import "luxe: world" for UI, World, UIEvent import "luxe: draw" for PathStyle import "luxe: assets" for Assets import "luxe: render" for Image import "luxe: math" for Math import "globals" for Globals import "math/rect" for AABB import "math/math" for M import "blocks/debug" for DrawDebug class UiCompass{ static font{"assets/fonts/BabyBlocks"} static size{8} static create(ent){ var compass = Control.create(ent) var style = PathStyle.new() style.color = [1,1,1,1] style.thickness = 2 Control.set_allow_input(compass, true) Control.set_state_data(compass, {"angle": 0, "style": style, "pressed": false}) //angle is in radians between 0 and tau Control.set_render(compass) {|control, state, x, y, w, h| var depth = UI.draw_depth_of(control, 0) var center = [x + w/2, y + h/2] var image = Assets.image("assets/wip/Needle") var ratio = Image.get_width(image) / Image.get_height(image) var needleSize = [(Math.min(w, h) - 8) * ratio, (Math.min(w, h) - 8)] UI.draw_ring(control, center.x, center.y, depth, w/2, h/2, 0, 360, 8, state["style"]) UI.draw_image(control, center.x - needleSize.x/2, center.y-needleSize.y/2, depth, needleSize.x, needleSize.y, state["angle"], [1,1,1,1], [0, 0, 1, 1], image, true) } Control.set_process(compass){|control, state, event, x,y,w,h| if(event.control != control) return if(event.type == UIEvent.press){ var relative_pos = AABB.grow(AABB.new(x, y, w, h),[2, 2]).relative_pos([event.x, event.y]) var inside = M.length(relative_pos.map{|comp| comp - 0.5}) < 0.5 if(inside) { state["pressed"] = true UI.capture(control) } } else if(event.type == UIEvent.release){ state["pressed"] = false UI.uncapture(control) } else if(event.type == UIEvent.move && state["pressed"]){ var center = [x + w/2, y + h/2] var diff = [center.x - event.x, center.y - event.y] var angle = Math.atan2(diff.x, diff.y) System.print("change %(Math.degrees(angle).round)") UI.events_emit(control, UIEvent.change, Math.degrees(angle)) } } return compass } static set_angle(con, angle){ var data = Control.get_state_data(con) data["angle"] = angle % 360 } static set_color(control, color){ var data = Control.get_state_data(control) data["style"].color = color } }