Added key event.

Some small additions/changes.
This commit is contained in:
Sander Schobers 2019-04-10 21:23:56 +02:00
parent 71cdc234ee
commit 67658d3bee
4 changed files with 82 additions and 8 deletions

View File

@ -60,6 +60,8 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) {
t.Handle(&ui.DisplayCloseEvent{EventBase: eventBase(e)})
case *allg5.DisplayResizeEvent:
t.Handle(&ui.DisplayResizeEvent{EventBase: eventBase(e), Bounds: geom.RectF32(float32(e.X), float32(e.Y), float32(e.X+e.Width), float32(e.Y+e.Height))})
case *allg5.KeyCharEvent:
t.Handle(&ui.KeyPressEvent{EventBase: eventBase(e), Key: key(e.KeyCode), Modifiers: keyModifiers(e.Modifiers), Character: e.UnicodeCharacter})
case *allg5.MouseButtonDownEvent:
t.Handle(&ui.MouseButtonDownEvent{MouseEvent: mouseEvent(e.MouseEvent), Button: ui.MouseButton(e.Button)})
case *allg5.MouseButtonUpEvent:
@ -257,6 +259,38 @@ func eventBase(e allg5.Event) ui.EventBase {
return ui.EventBase{StampInSeconds: e.Stamp()}
}
func key(key allg5.Key) ui.Key {
switch key {
case allg5.KeyBackspace:
return ui.KeyBackspace
case allg5.KeyDelete:
return ui.KeyDelete
case allg5.KeyDown:
return ui.KeyDown
case allg5.KeyEscape:
return ui.KeyEscape
case allg5.KeyLeft:
return ui.KeyLeft
case allg5.KeyRight:
return ui.KeyRight
case allg5.KeyUp:
return ui.KeyUp
}
return ui.KeyNone
}
func keyModifiers(mods allg5.KeyMod) ui.KeyModifier {
var m ui.KeyModifier
if mods&allg5.KeyModShift == allg5.KeyModShift {
m |= ui.KeyModifierShift
} else if mods&allg5.KeyModCtrl == allg5.KeyModCtrl {
m |= ui.KeyModifierControl
} else if mods&allg5.KeyModAlt == allg5.KeyModAlt {
m |= ui.KeyModifierAlt
}
return m
}
func mouseEvent(e allg5.MouseEvent) ui.MouseEvent {
return ui.MouseEvent{EventBase: eventBase(e), X: float32(e.X), Y: float32(e.Y)}
}

View File

@ -44,10 +44,13 @@ func (c *ControlBase) DesiredSize(Context) geom.PointF32 {
return geom.ZeroPtF32
}
func (c *ControlBase) mousePos(e MouseEvent) geom.PointF32 {
return e.Pos().Sub(c.offset)
}
func (c *ControlBase) Handle(ctx Context, e Event) {
var pos = func(e MouseEvent) geom.PointF32 { return e.Pos().Sub(c.offset) }
var over = func(e MouseEvent) bool {
c.over = pos(e).In(c.bounds)
c.over = c.mousePos(e).In(c.bounds)
return c.over
}
switch e := e.(type) {
@ -55,28 +58,28 @@ func (c *ControlBase) Handle(ctx Context, e Event) {
over(e.MouseEvent)
if c.pressed {
if c.dragStart == nil {
var start = pos(e.MouseEvent)
var start = c.mousePos(e.MouseEvent)
c.dragStart = &start
if c.onDragStart != nil {
c.onDragStart(ctx, c, start)
}
} else {
var start = *c.dragStart
var move = pos(e.MouseEvent)
var move = c.mousePos(e.MouseEvent)
if c.onDragMove != nil {
c.onDragMove(ctx, c, start, move)
}
}
}
case *MouseButtonDownEvent:
if over(e.MouseEvent) && 1 == e.Button {
if over(e.MouseEvent) && e.Button == MouseButtonLeft {
c.pressed = true
}
case *MouseButtonUpEvent:
if 1 == e.Button {
if e.Button == MouseButtonLeft {
if c.dragStart != nil {
var start = *c.dragStart
var end = pos(e.MouseEvent)
var end = c.mousePos(e.MouseEvent)
c.dragStart = nil
if c.onDragEnd != nil {
c.onDragEnd(ctx, c, start, end)
@ -84,7 +87,7 @@ func (c *ControlBase) Handle(ctx Context, e Event) {
}
if c.pressed {
if c.onClick != nil {
c.onClick(ctx, c, pos(e.MouseEvent), e.Button)
c.onClick(ctx, c, c.mousePos(e.MouseEvent), e.Button)
}
}
c.pressed = false

View File

@ -23,6 +23,35 @@ func (e *EventBase) Stamp() float64 {
return e.StampInSeconds
}
type Key int
const (
KeyNone Key = iota
KeyBackspace
KeyDelete
KeyDown
KeyEscape
KeyLeft
KeyRight
KeyUp
)
type KeyModifier int
const (
KeyModifiersNone KeyModifier = 0
KeyModifierShift = 1 << iota
KeyModifierControl
KeyModifierAlt
)
type KeyPressEvent struct {
EventBase
Key Key
Modifiers KeyModifier
Character rune
}
type MouseButton int
const (

View File

@ -7,6 +7,14 @@ type StackPanel struct {
Orientation Orientation
}
func BuildStackPanel(o Orientation, fn func(*StackPanel)) *StackPanel {
var p = &StackPanel{Orientation: o}
if fn != nil {
fn(p)
}
return p
}
func (p *StackPanel) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32) {
bounds = p.Orientation.FlipRect(bounds)
var length float32