From 27fc54d9a5c9bed2b5ad2cc876c7082bf3ae7bc2 Mon Sep 17 00:00:00 2001 From: Allen Ray Date: Tue, 23 Jun 2020 21:07:07 -0400 Subject: [PATCH] Adding some helper methods to handle vector conversion --- input.go | 28 ++++++++++++++++++++++++---- ui.go | 49 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/input.go b/input.go index 359eef8..7d413ed 100644 --- a/input.go +++ b/input.go @@ -7,7 +7,7 @@ import ( // prepareIO tells imgui.io about our current io state. func (ui *UI) prepareIO() { - ui.io.SetDisplaySize(pixelVecToimguiVec(ui.win.Bounds().Size())) + ui.io.SetDisplaySize(IVec(ui.win.Bounds().Size())) ui.io.AddMouseWheelDelta(float32(ui.win.MouseScroll().X), float32(ui.win.MouseScroll().Y)) mouse := ui.matrix.Unproject(ui.win.MousePosition()) @@ -30,7 +30,7 @@ func (ui *UI) prepareIO() { // updateKeyMod tells imgui.io where to find our key modifiers func (ui *UI) updateKeyMod() { - ui.io.KeyCtrl(int(pixelgl.KeyLeftControl), int(pixelgl.KeyRightControl)) + ui.io.KeyCtrl(int(pixelgl.KeyLeftAlt), int(pixelgl.KeyRightAlt)) ui.io.KeyShift(int(pixelgl.KeyLeftShift), int(pixelgl.KeyRightShift)) ui.io.KeyAlt(int(pixelgl.KeyLeftAlt), int(pixelgl.KeyRightAlt)) ui.io.KeySuper(int(pixelgl.KeyLeftSuper), int(pixelgl.KeyRightSuper)) @@ -65,6 +65,26 @@ func (ui *UI) Repeated(button pixelgl.Button) bool { return !ui.inputWant(button) && ui.win.Repeated(button) } +// KeyCtrl returns true if either left or right control is pressed +func (ui *UI) KeyCtrl() bool { + return ui.win.Pressed(pixelgl.KeyLeftControl) || ui.win.Pressed(pixelgl.KeyRightControl) +} + +// KeyCtrl returns true if either left or right shift is pressed +func (ui *UI) KeyShift() bool { + return ui.win.Pressed(pixelgl.KeyLeftShift) || ui.win.Pressed(pixelgl.KeyRightShift) +} + +// KeyCtrl returns true if either left or right alt is pressed +func (ui *UI) KeyAlt() bool { + return ui.win.Pressed(pixelgl.KeyLeftAlt) || ui.win.Pressed(pixelgl.KeyRightAlt) +} + +// KeyCtrl returns true if either left or right super (windows key) is pressed +func (ui *UI) KeySuper() bool { + return ui.win.Pressed(pixelgl.KeyLeftSuper) || ui.win.Pressed(pixelgl.KeyRightSuper) +} + // setKeyMapping maps pixelgl buttons to imgui keys. func (ui *UI) setKeyMapping() { keys := map[int]pixelgl.Button{ @@ -210,11 +230,11 @@ var keys = []pixelgl.Button{ pixelgl.KeyKPEnter, pixelgl.KeyKPEqual, pixelgl.KeyLeftShift, - pixelgl.KeyLeftControl, + pixelgl.KeyLeftAlt, pixelgl.KeyLeftAlt, pixelgl.KeyLeftSuper, pixelgl.KeyRightShift, - pixelgl.KeyRightControl, + pixelgl.KeyRightAlt, pixelgl.KeyRightAlt, pixelgl.KeyRightSuper, pixelgl.KeyMenu, diff --git a/ui.go b/ui.go index 0a7a8e9..51926b2 100644 --- a/ui.go +++ b/ui.go @@ -45,6 +45,8 @@ type UI struct { matrix pixel.Matrix } +var currentUI *UI + // pixelui.NewUI flags: // NO_DEFAULT_FONT: Do not load the default font during NewUI. const ( @@ -62,11 +64,12 @@ func NewUI(win *pixelgl.Window, flags int) *UI { win: win, context: context, } + currentUI = ui ui.matrix = pixel.IM.ScaledXY(win.Bounds().Center(), pixel.V(1, -1)) ui.io = imgui.CurrentIO() - ui.io.SetDisplaySize(pixelVecToimguiVec(win.Bounds().Size())) + ui.io.SetDisplaySize(IVec(win.Bounds().Size())) ui.fonts = ui.io.Fonts() @@ -137,9 +140,9 @@ func (ui *UI) Draw(win *pixelgl.Window) { uv := (*imgui.Vec2)(unsafe.Pointer(uintptr(ptr) + uintptr(uvOffset))) col := (*uint32)(unsafe.Pointer(uintptr(ptr) + uintptr(colOffset))) - position := imguiVecToPixelVec(*pos) + position := PV(*pos) color := imguiColorToPixelColor(*col) - uuvv := imguiVecToPixelVec(*uv) + uuvv := PV(*uv) (*tris)[i].Position = position (*tris)[i].Picture = uuvv @@ -172,16 +175,44 @@ func imguiColorToPixelColor(c uint32) color.RGBA { } } -// imguiVecToPixelVec Converts the imgui vector to a Pixel vector -func imguiVecToPixelVec(v imgui.Vec2) pixel.Vec { - return pixel.V(float64(v.X), float64(v.Y)) -} - // imguiRectToPixelRect Converts the imgui rect to a Pixel rect func imguiRectToPixelRect(r imgui.Vec4) pixel.Rect { return pixel.R(float64(r.X), float64(r.Y), float64(r.Z), float64(r.W)) } -func pixelVecToimguiVec(v pixel.Vec) imgui.Vec2 { +// IVec converts a pixel vector to an imgui vector +func IVec(v pixel.Vec) imgui.Vec2 { return imgui.Vec2{X: float32(v.X), Y: float32(v.Y)} } + +// IV creates an imgui vector from the given points. +func IV(x, y float64) imgui.Vec2 { + return imgui.Vec2{X: float32(x), Y: float32(y)} +} + +// PV converts an imgui vector to a pixel vector +func PV(v imgui.Vec2) pixel.Vec { + return pixel.V(float64(v.X), float64(v.Y)) +} + +// ProjectVec projects the vector by the UI's matrix (vertical flip) +// and returns that as a imgui vector +func ProjectVec(v pixel.Vec) imgui.Vec2 { + return IVec(currentUI.matrix.Project(v)) +} + +// ProjectV creates a pixel vector and projects it using ProjectVec +func ProjectV(x, y float64) imgui.Vec2 { + return ProjectVec(pixel.V(x, y)) +} + +// UnprojectV unprojects the vector by the UI's matrix (vertical flip) +// and returns that as a pixel vector +func UnprojectV(v imgui.Vec2) pixel.Vec { + return currentUI.matrix.Unproject(PV(v)) +} + +// IZV returns an imgui zero vector +func IZV() imgui.Vec2 { + return imgui.Vec2{X: 0, Y: 0} +}