diff --git a/gui/buffer.go b/gui/buffer.go index b6e1138..2105d68 100644 --- a/gui/buffer.go +++ b/gui/buffer.go @@ -34,7 +34,6 @@ type camera struct { type Buffer struct { BaseComponent - HasFocus bool index int parent *View font *strife.Font @@ -793,7 +792,7 @@ func (b *Buffer) OnUpdate() bool { } func (b *Buffer) doUpdate(pred func(r int) bool) bool { - if !b.HasFocus { + if !b.HasFocus() { return false } @@ -843,8 +842,7 @@ func (b *Buffer) doUpdate(pred func(r int) bool) bool { } } - // FIXME handle focus properly - if b.inputHandler == nil { + if !b.HasFocus() { return false } @@ -922,7 +920,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { ctx.SetColor(strife.HexRGB(b.cfg.Theme.Background)) ctx.Rect(b.x, b.y, b.w, b.h, strife.Fill) - if b.cfg.Editor.Highlight_Line && b.HasFocus { + if b.cfg.Editor.Highlight_Line && b.HasFocus() { ctx.SetColor(strife.Black) // highlight_line_col? highlightLinePosY := ey + (ry + b.curs.ry*last_h) - (b.cam.y * last_h) @@ -998,7 +996,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { // if we're currently over a character then set // the font colour to something else // ONLY SET THE COLOUR IF WE HAVE FOCUS ALSO! - if b.HasFocus && b.curs.x+1 == x_col && b.curs.y == y_col && should_draw { + if b.HasFocus() && b.curs.x+1 == x_col && b.curs.y == y_col && should_draw { ctx.SetColor(strife.HexRGB(b.cfg.Theme.Cursor_Invert)) } @@ -1035,7 +1033,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { } // render the ol' cursor - if b.HasFocus && renderFlashingCursor && b.cfg.Cursor.Draw { + if b.HasFocus() && renderFlashingCursor && b.cfg.Cursor.Draw { cursorWidth := b.cfg.Cursor.GetCaretWidth() if cursorWidth == -1 { cursorWidth = last_w diff --git a/gui/component.go b/gui/component.go index b1d32b8..5eece3c 100644 --- a/gui/component.go +++ b/gui/component.go @@ -20,14 +20,22 @@ type Component interface { HandleEvent(evt strife.StrifeEvent) - GetInputHandler() *InputHandler - SetInputHandler(h *InputHandler) + SetFocus(focus bool) + HasFocus() bool } type BaseComponent struct { - x, y int - w, h int - inputHandler *InputHandler + x, y int + w, h int + focused bool +} + +func (b *BaseComponent) SetFocus(focus bool) { + b.focused = focus +} + +func (b *BaseComponent) HasFocus() bool { + return b.focused } func (b *BaseComponent) HandleEvent(evt strife.StrifeEvent) { @@ -48,11 +56,3 @@ func (b *BaseComponent) Resize(w, h int) { b.w = w b.h = h } - -func (b *BaseComponent) SetInputHandler(i *InputHandler) { - b.inputHandler = i -} - -func (b *BaseComponent) GetInputHandler() *InputHandler { - return b.inputHandler -} diff --git a/gui/input.go b/gui/input.go deleted file mode 100644 index 61e8eea..0000000 --- a/gui/input.go +++ /dev/null @@ -1,20 +0,0 @@ -package gui - -import ( - "github.com/felixangell/strife" - "github.com/veandco/go-sdl2/sdl" -) - -type InputHandler struct { - Event sdl.Event -} - -func HandleEvent(comp Component, evt strife.StrifeEvent) { - comp.HandleEvent(evt) - for _, child := range comp.GetComponents() { - if child == nil { - continue - } - child.HandleEvent(evt) - } -} diff --git a/gui/palette.go b/gui/palette.go index 7976574..8d8376e 100644 --- a/gui/palette.go +++ b/gui/palette.go @@ -10,7 +10,6 @@ import ( type CommandPalette struct { BaseComponent - HasFocus bool buff *Buffer parentBuff *Buffer } @@ -22,7 +21,6 @@ func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette { palette := &CommandPalette{ buff: NewBuffer(&conf, nil, 0), parentBuff: nil, - HasFocus: false, } palette.buff.appendLine("") @@ -57,8 +55,8 @@ func (b *CommandPalette) clearInput() { } func (b *CommandPalette) OnUpdate() bool { - if !b.HasFocus { - return true + if !b.HasFocus() { + return false } override := func(k int) bool { @@ -74,7 +72,7 @@ func (b *CommandPalette) OnUpdate() bool { } func (b *CommandPalette) OnRender(ctx *strife.Renderer) { - if !b.HasFocus { + if !b.HasFocus() { return } diff --git a/gui/panel.go b/gui/panel.go deleted file mode 100644 index 511cc3c..0000000 --- a/gui/panel.go +++ /dev/null @@ -1,25 +0,0 @@ -package gui - -import ( - "github.com/felixangell/strife" -) - -type Panel struct { - BaseComponent -} - -func NewPanel(input *InputHandler) *Panel { - panel := &Panel{} - panel.SetInputHandler(input) - return panel -} - -func (p *Panel) OnDispose() {} - -func (p *Panel) OnInit() {} - -func (p *Panel) OnUpdate() bool { - return false -} - -func (p *Panel) OnRender(ctx *strife.Renderer) {} diff --git a/gui/view.go b/gui/view.go index 88c3d42..6631427 100644 --- a/gui/view.go +++ b/gui/view.go @@ -1,6 +1,7 @@ package gui import ( + "fmt" "github.com/felixangell/phi/cfg" "github.com/felixangell/strife" ) @@ -33,37 +34,32 @@ func NewView(width, height int, conf *cfg.TomlConfig) *View { func (n *View) hidePalette() { p := n.commandPalette p.clearInput() - p.HasFocus = false + p.SetFocus(false) // set focus to the buffer // that invoked the cmd palette - p.parentBuff.inputHandler = p.buff.inputHandler - p.parentBuff.HasFocus = true + p.parentBuff.SetFocus(true) // remove focus from palette - p.buff.HasFocus = false - p.buff.SetInputHandler(nil) + p.buff.SetFocus(false) } func (n *View) focusPalette(buff *Buffer) { p := n.commandPalette - p.HasFocus = true + p.SetFocus(true) // focus the command palette - p.buff.HasFocus = true - p.buff.SetInputHandler(buff.inputHandler) + p.buff.SetFocus(true) // remove focus from the buffer // that invoked the command palette - buff.inputHandler = nil p.parentBuff = buff } func (n *View) UnfocusBuffers() { // clear focus from buffers for _, buff := range n.buffers { - buff.HasFocus = false - buff.inputHandler = nil + buff.SetFocus(false) } } @@ -77,7 +73,24 @@ func sign(dir int) int { } func (n *View) ChangeFocus(dir int) { - println("implement me! ", dir) + prevBuff, _ := n.buffers[n.focusedBuff] + + if dir == -1 { + n.focusedBuff-- + } else if dir == 1 { + n.focusedBuff++ + } + + if n.focusedBuff < 0 { + n.focusedBuff = len(n.buffers) - 1 + } else if n.focusedBuff >= len(n.buffers) { + n.focusedBuff = 0 + } + + prevBuff.SetFocus(false) + if buff, ok := n.buffers[n.focusedBuff]; ok { + buff.SetFocus(true) + } } func (n *View) OnInit() { @@ -86,20 +99,22 @@ func (n *View) OnInit() { func (n *View) OnUpdate() bool { dirty := false - for _, buffer := range n.buffers { - if buffer.OnUpdate() { - dirty = true - } + if buff, ok := n.buffers[n.focusedBuff]; ok { + buff.OnUpdate() } + n.commandPalette.OnUpdate() return dirty } func (n *View) OnRender(ctx *strife.Renderer) { - for _, buffer := range n.buffers { + for idx, buffer := range n.buffers { buffer.OnRender(ctx) + + ctx.String(fmt.Sprintf("idx %d", idx), (buffer.x+buffer.w)-150, (buffer.y+buffer.h)-150) } + n.commandPalette.OnRender(ctx) } @@ -107,11 +122,11 @@ func (n *View) OnDispose() {} func (n *View) AddBuffer() *Buffer { if buf, ok := n.buffers[n.focusedBuff]; ok { - buf.HasFocus = false + buf.SetFocus(false) } c := NewBuffer(n.conf, n, len(n.buffers)) - c.HasFocus = true + c.SetFocus(true) // work out the size of the buffer and set it // note that we +1 the components because