Skip to content

Commit

Permalink
Adding some helper methods to handle vector conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Jun 24, 2020
1 parent 3fcdd79 commit 27fc54d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
28 changes: 24 additions & 4 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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))
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down
49 changes: 40 additions & 9 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
}

0 comments on commit 27fc54d

Please sign in to comment.