mirror of
https://github.com/felixangell/phi.git
synced 2024-09-11 15:55:25 +03:00
Cleaned up component abstractions... added command palette that does nothing
This commit is contained in:
parent
af967a37fb
commit
03aa461dd8
@ -49,13 +49,13 @@ func NewBuffer(conf *cfg.TomlConfig) *Buffer {
|
||||
return buff
|
||||
}
|
||||
|
||||
func (b *Buffer) Dispose() {
|
||||
func (b *Buffer) OnDispose() {
|
||||
for _, texture := range TEXTURE_CACHE {
|
||||
texture.Destroy()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) Init() {}
|
||||
func (b *Buffer) OnInit() {}
|
||||
|
||||
func (b *Buffer) appendLine(val string) {
|
||||
b.contents = append(b.contents, rope.New(val))
|
||||
@ -246,12 +246,13 @@ func renderString(font *ttf.Font, val string, col sdl.Color, smooth bool) *sdl.S
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Buffer) Update() {
|
||||
func (b *Buffer) OnUpdate() {
|
||||
prev_x := b.curs.x
|
||||
prev_y := b.curs.y
|
||||
|
||||
// FIXME handle focus properly
|
||||
if b.inputHandler == nil {
|
||||
panic("help")
|
||||
return
|
||||
}
|
||||
|
||||
if b.inputHandler.Event != nil {
|
||||
|
@ -8,10 +8,10 @@ type Component interface {
|
||||
Translate(x, y int32)
|
||||
Resize(w, h int32)
|
||||
|
||||
Init()
|
||||
Update()
|
||||
OnInit()
|
||||
OnUpdate()
|
||||
OnRender(*sdl.Renderer)
|
||||
Dispose()
|
||||
OnDispose()
|
||||
|
||||
AddComponent(c Component)
|
||||
GetComponents() []Component
|
||||
@ -27,13 +27,6 @@ type BaseComponent struct {
|
||||
inputHandler *InputHandler
|
||||
}
|
||||
|
||||
func Render(c Component, ctx *sdl.Renderer) {
|
||||
c.OnRender(ctx)
|
||||
for _, child := range c.GetComponents() {
|
||||
Render(child, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BaseComponent) Translate(x, y int32) {
|
||||
b.x += x
|
||||
b.y += y
|
||||
@ -53,6 +46,8 @@ func (b *BaseComponent) GetComponents() []Component {
|
||||
|
||||
func (b *BaseComponent) AddComponent(c Component) {
|
||||
b.components = append(b.components, c)
|
||||
c.SetInputHandler(b.inputHandler)
|
||||
Init(c)
|
||||
}
|
||||
|
||||
func (b *BaseComponent) SetInputHandler(i *InputHandler) {
|
||||
@ -62,3 +57,31 @@ func (b *BaseComponent) SetInputHandler(i *InputHandler) {
|
||||
func (b *BaseComponent) GetInputHandler() *InputHandler {
|
||||
return b.inputHandler
|
||||
}
|
||||
|
||||
func Update(c Component) {
|
||||
c.OnUpdate()
|
||||
for _, child := range c.GetComponents() {
|
||||
Update(child)
|
||||
}
|
||||
}
|
||||
|
||||
func Render(c Component, ctx *sdl.Renderer) {
|
||||
c.OnRender(ctx)
|
||||
for _, child := range c.GetComponents() {
|
||||
Render(child, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func Init(c Component) {
|
||||
c.OnInit()
|
||||
for _, child := range c.GetComponents() {
|
||||
Init(child)
|
||||
}
|
||||
}
|
||||
|
||||
func Dispose(c Component) {
|
||||
c.OnDispose()
|
||||
for _, child := range c.GetComponents() {
|
||||
Dispose(child)
|
||||
}
|
||||
}
|
||||
|
@ -4,33 +4,42 @@ import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
type CommandBuffer struct {
|
||||
*Buffer
|
||||
}
|
||||
|
||||
func (b *CommandBuffer) processActionKey(t *sdl.KeyDownEvent) {
|
||||
switch t.Keysym.Scancode {
|
||||
case sdl.SCANCODE_RETURN:
|
||||
println("dope!")
|
||||
return
|
||||
}
|
||||
b.Buffer.processActionKey(t)
|
||||
}
|
||||
|
||||
type CommandPalette struct {
|
||||
BaseComponent
|
||||
buff *Buffer
|
||||
buffer *CommandBuffer
|
||||
}
|
||||
|
||||
func NewCommandPalette() *CommandPalette {
|
||||
palette := &CommandPalette{
|
||||
buff: NewBuffer(nil),
|
||||
palette := &CommandPalette{}
|
||||
palette.buffer = &CommandBuffer{
|
||||
Buffer: NewBuffer(nil),
|
||||
}
|
||||
palette.AddComponent(palette.buffer)
|
||||
return palette
|
||||
}
|
||||
|
||||
func (p *CommandPalette) Dispose() {
|
||||
for _, comp := range p.components {
|
||||
comp.Dispose()
|
||||
}
|
||||
func (p *CommandPalette) OnDispose() {}
|
||||
|
||||
func (p *CommandPalette) OnInit() {
|
||||
}
|
||||
|
||||
func (p *CommandPalette) Init() {
|
||||
p.buff.SetInputHandler(p.inputHandler)
|
||||
p.AddComponent(p.buff)
|
||||
func (c *CommandPalette) OnUpdate() {
|
||||
|
||||
}
|
||||
|
||||
func (c *CommandPalette) Update() {
|
||||
for _, c := range c.components {
|
||||
c.Update()
|
||||
}
|
||||
}
|
||||
func (c *CommandPalette) OnRender(ctx *sdl.Renderer) {
|
||||
|
||||
func (c *CommandPalette) OnRender(ctx *sdl.Renderer) {}
|
||||
}
|
||||
|
17
gui/panel.go
17
gui/panel.go
@ -14,19 +14,10 @@ func NewPanel(input *InputHandler) *Panel {
|
||||
return panel
|
||||
}
|
||||
|
||||
func (p *Panel) Dispose() {
|
||||
for _, comp := range p.components {
|
||||
comp.Dispose()
|
||||
}
|
||||
}
|
||||
func (p *Panel) OnDispose() {}
|
||||
|
||||
func (p *Panel) Init() {}
|
||||
func (p *Panel) OnInit() {}
|
||||
|
||||
func (p *Panel) Update() {
|
||||
for _, c := range p.components {
|
||||
c.Update()
|
||||
}
|
||||
}
|
||||
func (p *Panel) OnUpdate() {}
|
||||
|
||||
func (p *Panel) OnRender(ctx *sdl.Renderer) {
|
||||
}
|
||||
func (p *Panel) OnRender(ctx *sdl.Renderer) {}
|
||||
|
45
gui/view.go
45
gui/view.go
@ -1,4 +1,49 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"github.com/felixangell/nate/cfg"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
type View struct {
|
||||
BaseComponent
|
||||
|
||||
conf *cfg.TomlConfig
|
||||
}
|
||||
|
||||
func NewView(width, height int, conf *cfg.TomlConfig) *View {
|
||||
view := &View{conf: conf}
|
||||
view.Translate(int32(width), int32(height))
|
||||
return view
|
||||
}
|
||||
|
||||
func (n *View) OnInit() {
|
||||
n.addBuffer()
|
||||
}
|
||||
|
||||
func (n *View) OnUpdate() {}
|
||||
|
||||
func (n *View) OnRender(ctx *sdl.Renderer) {}
|
||||
|
||||
func (n *View) OnDispose() {}
|
||||
|
||||
func (n *View) addBuffer() {
|
||||
c := NewBuffer(n.conf)
|
||||
|
||||
// work out the size of the buffer and set it
|
||||
// note that we +1 the components because
|
||||
// we haven't yet added the panel
|
||||
bufferWidth := n.w / int32(len(n.components)+1)
|
||||
c.Resize(bufferWidth, n.h)
|
||||
|
||||
// setup and add the panel for the buffer
|
||||
panel := NewPanel(n.inputHandler)
|
||||
c.SetInputHandler(n.inputHandler)
|
||||
panel.AddComponent(c)
|
||||
n.components = append(n.components, panel)
|
||||
|
||||
// translate all the components accordingly.
|
||||
for i, p := range n.components {
|
||||
p.Translate(int32(bufferWidth)*int32(i), 0)
|
||||
}
|
||||
}
|
||||
|
67
main.go
67
main.go
@ -16,78 +16,54 @@ const (
|
||||
)
|
||||
|
||||
type NateEditor struct {
|
||||
window *sdl.Window
|
||||
renderer *sdl.Renderer
|
||||
running bool
|
||||
bufferPanels []*gui.Panel
|
||||
inputHandler *gui.InputHandler
|
||||
}
|
||||
gui.BaseComponent
|
||||
|
||||
func (n *NateEditor) addBuffer(c gui.Component) {
|
||||
// work out the size of the buffer and set it
|
||||
// note that we +1 the bufferPanels because
|
||||
// we haven't yet added the panel
|
||||
w, height := n.window.GetSize()
|
||||
bufferWidth := w / (len(n.bufferPanels) + 1)
|
||||
c.Resize(int32(bufferWidth), int32(height))
|
||||
|
||||
// setup and add the panel for the buffer
|
||||
panel := gui.NewPanel(n.inputHandler)
|
||||
c.SetInputHandler(n.inputHandler)
|
||||
panel.AddComponent(c)
|
||||
n.bufferPanels = append(n.bufferPanels, panel)
|
||||
|
||||
// translate all the panels accordingly.
|
||||
for i, p := range n.bufferPanels {
|
||||
p.Translate(int32(bufferWidth)*int32(i), 0)
|
||||
}
|
||||
window *sdl.Window
|
||||
renderer *sdl.Renderer
|
||||
running bool
|
||||
}
|
||||
|
||||
func (n *NateEditor) init(cfg *cfg.TomlConfig) {
|
||||
n.addBuffer(gui.NewBuffer(cfg))
|
||||
w, h := n.window.GetSize()
|
||||
n.AddComponent(gui.NewView(w, h, cfg))
|
||||
|
||||
/*
|
||||
bufferPanel := gui.NewPanel(n.inputHandler)
|
||||
palette := gui.NewCommandPalette()
|
||||
palette.SetInputHandler(n.inputHandler)
|
||||
bufferPanel.AddComponent(palette)
|
||||
n.panels = append(n.panels, bufferPanel)
|
||||
*/
|
||||
palette := gui.NewCommandPalette()
|
||||
palette.Translate(int32(w/2), 20)
|
||||
n.AddComponent(palette)
|
||||
}
|
||||
|
||||
func (n *NateEditor) dispose() {
|
||||
for _, buffer := range n.bufferPanels {
|
||||
buffer.Dispose()
|
||||
for _, comp := range n.GetComponents() {
|
||||
gui.Dispose(comp)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NateEditor) update() {
|
||||
for _, panel := range n.bufferPanels {
|
||||
panel.Update()
|
||||
}
|
||||
|
||||
n.inputHandler.Event = nil
|
||||
n.GetInputHandler().Event = nil
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
n.inputHandler.Event = event
|
||||
n.GetInputHandler().Event = event
|
||||
|
||||
switch event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
n.running = false
|
||||
case *sdl.TextEditingEvent:
|
||||
n.inputHandler.Event = event
|
||||
n.GetInputHandler().Event = event
|
||||
case *sdl.TextInputEvent:
|
||||
n.inputHandler.Event = event
|
||||
n.GetInputHandler().Event = event
|
||||
}
|
||||
}
|
||||
|
||||
for _, comp := range n.GetComponents() {
|
||||
gui.Update(comp)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NateEditor) render() {
|
||||
gfx.SetDrawColorHex(n.renderer, 0xffffff)
|
||||
n.renderer.Clear()
|
||||
|
||||
for _, panel := range n.bufferPanels {
|
||||
gui.Render(panel, n.renderer)
|
||||
for _, component := range n.GetComponents() {
|
||||
gui.Render(component, n.renderer)
|
||||
}
|
||||
|
||||
n.renderer.Present()
|
||||
@ -146,7 +122,8 @@ func main() {
|
||||
}
|
||||
defer renderer.Destroy()
|
||||
|
||||
editor := &NateEditor{window: window, renderer: renderer, running: true, inputHandler: &gui.InputHandler{}}
|
||||
editor := &NateEditor{window: window, renderer: renderer, running: true}
|
||||
editor.SetInputHandler(&gui.InputHandler{})
|
||||
editor.init(&config)
|
||||
|
||||
timer := sdl.GetTicks()
|
||||
|
Loading…
Reference in New Issue
Block a user