mirror of
https://github.com/felixangell/phi.git
synced 2024-09-11 15:55:25 +03:00
fmt codebase
This commit is contained in:
parent
1ae5e9afe3
commit
b496fe896c
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
nate
|
||||
fmt.sh
|
||||
|
11
cfg/config.go
Normal file
11
cfg/config.go
Normal file
@ -0,0 +1,11 @@
|
||||
package cfg
|
||||
|
||||
type Config struct {
|
||||
Aliased bool
|
||||
}
|
||||
|
||||
func NewDefaultConfig() *Config {
|
||||
return &Config{
|
||||
Aliased: false,
|
||||
}
|
||||
}
|
12
gfx/col.go
12
gfx/col.go
@ -4,16 +4,16 @@ import "github.com/veandco/go-sdl2/sdl"
|
||||
|
||||
func SetDrawColorHex(ctx *sdl.Renderer, col uint32) {
|
||||
a := uint8(255)
|
||||
r := uint8(col&0xff0000 >> 16)
|
||||
g := uint8(col&0xff00 >> 8)
|
||||
b := uint8(col&0xff)
|
||||
r := uint8(col & 0xff0000 >> 16)
|
||||
g := uint8(col & 0xff00 >> 8)
|
||||
b := uint8(col & 0xff)
|
||||
ctx.SetDrawColor(r, g, b, a)
|
||||
}
|
||||
|
||||
func HexColor(col uint32) sdl.Color {
|
||||
a := uint8(255)
|
||||
r := uint8(col&0xff0000 >> 16)
|
||||
g := uint8(col&0xff00 >> 8)
|
||||
b := uint8(col&0xff)
|
||||
r := uint8(col & 0xff0000 >> 16)
|
||||
g := uint8(col & 0xff00 >> 8)
|
||||
b := uint8(col & 0xff)
|
||||
return sdl.Color{r, g, b, a}
|
||||
}
|
||||
|
@ -1,15 +1,16 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"github.com/felixangell/nate/cfg"
|
||||
"github.com/felixangell/nate/gfx"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/veandco/go-sdl2/sdl_ttf"
|
||||
"github.com/vinzmay/go-rope"
|
||||
"github.com/felixangell/nate/gfx"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Cursor struct {
|
||||
x, y int
|
||||
x, y int
|
||||
rx, ry int
|
||||
}
|
||||
|
||||
@ -29,25 +30,26 @@ func (c *Cursor) move_render(x, y, rx, ry int) {
|
||||
|
||||
const (
|
||||
cursor_flash_ms uint32 = 400
|
||||
reset_delay_ms uint32 = 400
|
||||
reset_delay_ms uint32 = 400
|
||||
)
|
||||
|
||||
var (
|
||||
should_draw bool = false
|
||||
should_flash bool = true
|
||||
timer uint32 = 0
|
||||
reset_timer uint32 = 0
|
||||
should_draw bool = false
|
||||
should_flash bool = true
|
||||
timer uint32 = 0
|
||||
reset_timer uint32 = 0
|
||||
)
|
||||
|
||||
type Buffer struct {
|
||||
x, y int
|
||||
font *ttf.Font
|
||||
contents []*rope.Rope
|
||||
curs *Cursor
|
||||
x, y int
|
||||
font *ttf.Font
|
||||
contents []*rope.Rope
|
||||
curs *Cursor
|
||||
input_handler *InputHandler
|
||||
cfg *cfg.Config
|
||||
}
|
||||
|
||||
func NewBuffer() *Buffer {
|
||||
func NewBuffer(conf *cfg.Config) *Buffer {
|
||||
font, err := ttf.OpenFont("./res/firacode.ttf", 24)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -55,8 +57,9 @@ func NewBuffer() *Buffer {
|
||||
|
||||
buff := &Buffer{
|
||||
contents: []*rope.Rope{},
|
||||
font: font,
|
||||
curs: &Cursor{},
|
||||
font: font,
|
||||
curs: &Cursor{},
|
||||
cfg: conf,
|
||||
}
|
||||
buff.appendLine("This is a test.")
|
||||
return buff
|
||||
@ -94,13 +97,30 @@ func (b *Buffer) processActionKey(t *sdl.KeyDownEvent) {
|
||||
b.curs.move(line_len, 1)
|
||||
b.contents = append(b.contents, rope.New(" "))
|
||||
case sdl.SCANCODE_BACKSPACE:
|
||||
if (b.curs.x > 0) {
|
||||
if b.curs.x > 0 {
|
||||
b.contents[b.curs.y] = b.contents[b.curs.y].Delete(b.curs.x, 1)
|
||||
b.curs.move(-1, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func renderString(font *ttf.Font, val string, col sdl.Color, smooth bool) *sdl.Surface {
|
||||
if smooth {
|
||||
text, err := font.RenderUTF8_Blended(val, col)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return text
|
||||
} else {
|
||||
text, err := font.RenderUTF8_Solid(val, col)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return text
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Buffer) Update() {
|
||||
prev_x := b.curs.x
|
||||
prev_y := b.curs.y
|
||||
@ -120,33 +140,34 @@ func (b *Buffer) Update() {
|
||||
reset_timer = sdl.GetTicks()
|
||||
}
|
||||
|
||||
if !should_flash && sdl.GetTicks() - reset_timer > reset_delay_ms {
|
||||
if !should_flash && sdl.GetTicks()-reset_timer > reset_delay_ms {
|
||||
should_flash = true
|
||||
}
|
||||
|
||||
if sdl.GetTicks() - timer > cursor_flash_ms && should_flash {
|
||||
if sdl.GetTicks()-timer > cursor_flash_ms && should_flash {
|
||||
timer = sdl.GetTicks()
|
||||
should_draw = !should_draw
|
||||
}
|
||||
}
|
||||
|
||||
var last_w, last_h int32
|
||||
|
||||
func (b *Buffer) Render(ctx *sdl.Renderer) {
|
||||
|
||||
// render the ol' cursor
|
||||
if (should_draw) {
|
||||
if should_draw {
|
||||
gfx.SetDrawColorHex(ctx, 0x657B83)
|
||||
ctx.FillRect(&sdl.Rect{
|
||||
(int32(b.curs.rx) + 1) * last_w,
|
||||
int32(b.curs.ry) * last_h,
|
||||
last_w,
|
||||
(int32(b.curs.rx) + 1) * last_w,
|
||||
int32(b.curs.ry) * last_h,
|
||||
last_w,
|
||||
last_h,
|
||||
})
|
||||
}
|
||||
|
||||
var y_col int32
|
||||
for _, rope := range b.contents {
|
||||
|
||||
|
||||
var x_col int32
|
||||
for _, char := range rope.String() {
|
||||
switch char {
|
||||
@ -158,23 +179,20 @@ func (b *Buffer) Render(ctx *sdl.Renderer) {
|
||||
|
||||
x_col += 1
|
||||
|
||||
text, err := b.font.RenderUTF8_Solid(string(char), gfx.HexColor(0x7a7a7a))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
text := renderString(b.font, string(char), gfx.HexColor(0x7a7a7a), b.cfg.Aliased)
|
||||
defer text.Free()
|
||||
|
||||
last_w = text.W
|
||||
last_h = text.H
|
||||
|
||||
// FIXME very slow
|
||||
texture, err := ctx.CreateTextureFromSurface(text)
|
||||
texture, _ := ctx.CreateTextureFromSurface(text)
|
||||
defer texture.Destroy()
|
||||
|
||||
ctx.Copy(texture, nil, &sdl.Rect{
|
||||
(x_col * text.W),
|
||||
(y_col * text.H),
|
||||
text.W,
|
||||
(x_col * text.W),
|
||||
(y_col * text.H),
|
||||
text.W,
|
||||
text.H,
|
||||
})
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
type Component interface {
|
||||
Update()
|
||||
Render(*sdl.Renderer)
|
||||
|
||||
|
||||
GetInputHandler() *InputHandler
|
||||
SetInputHandler(h *InputHandler)
|
||||
}
|
||||
|
@ -5,13 +5,13 @@ import (
|
||||
)
|
||||
|
||||
type Panel struct {
|
||||
components []Component
|
||||
components []Component
|
||||
input_handler *InputHandler
|
||||
}
|
||||
|
||||
func NewPanel(input *InputHandler) *Panel {
|
||||
return &Panel{
|
||||
components: []Component{},
|
||||
components: []Component{},
|
||||
input_handler: input,
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
package gui
|
||||
|
||||
type View struct {
|
||||
|
||||
}
|
||||
|
125
main.go
125
main.go
@ -1,103 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/veandco/go-sdl2/sdl_ttf"
|
||||
"github.com/felixangell/nate/gui"
|
||||
"fmt"
|
||||
"github.com/felixangell/nate/cfg"
|
||||
"github.com/felixangell/nate/gfx"
|
||||
"fmt"
|
||||
"github.com/felixangell/nate/gui"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/veandco/go-sdl2/sdl_ttf"
|
||||
)
|
||||
|
||||
const (
|
||||
PRINT_FPS bool = false
|
||||
PRINT_FPS bool = false
|
||||
)
|
||||
|
||||
type NateEditor struct {
|
||||
window *sdl.Window
|
||||
renderer *sdl.Renderer
|
||||
running bool
|
||||
panels []*gui.Panel
|
||||
input_handler *gui.InputHandler
|
||||
window *sdl.Window
|
||||
renderer *sdl.Renderer
|
||||
running bool
|
||||
panels []*gui.Panel
|
||||
input_handler *gui.InputHandler
|
||||
}
|
||||
|
||||
func (n *NateEditor) init() {
|
||||
// setup a default panel
|
||||
testPanel := gui.NewPanel(n.input_handler)
|
||||
testPanel.AddComponent(gui.NewBuffer())
|
||||
n.panels = append(n.panels, testPanel)
|
||||
// setup a default panel
|
||||
testPanel := gui.NewPanel(n.input_handler)
|
||||
testPanel.AddComponent(gui.NewBuffer(cfg.NewDefaultConfig()))
|
||||
n.panels = append(n.panels, testPanel)
|
||||
}
|
||||
|
||||
func (n *NateEditor) update() {
|
||||
n.input_handler.Event = nil
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
n.input_handler.Event = event
|
||||
|
||||
switch event.(type) {
|
||||
|
||||
switch event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
n.running = false
|
||||
case *sdl.TextInputEvent:
|
||||
n.input_handler.Event = event
|
||||
n.input_handler.Event = event
|
||||
}
|
||||
}
|
||||
|
||||
for _, panel := range n.panels {
|
||||
panel.Update()
|
||||
}
|
||||
for _, panel := range n.panels {
|
||||
panel.Update()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NateEditor) render() {
|
||||
gfx.SetDrawColorHex(n.renderer, 0xfdf6e3)
|
||||
n.renderer.Clear()
|
||||
gfx.SetDrawColorHex(n.renderer, 0xfdf6e3)
|
||||
n.renderer.Clear()
|
||||
|
||||
for _, panel := range n.panels {
|
||||
panel.Render(n.renderer)
|
||||
}
|
||||
for _, panel := range n.panels {
|
||||
panel.Render(n.renderer)
|
||||
}
|
||||
|
||||
n.renderer.Present()
|
||||
n.renderer.Present()
|
||||
}
|
||||
|
||||
func main() {
|
||||
sdl.Init(sdl.INIT_EVERYTHING)
|
||||
defer sdl.Quit()
|
||||
sdl.Init(sdl.INIT_EVERYTHING)
|
||||
defer sdl.Quit()
|
||||
|
||||
if err := ttf.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := ttf.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
window, err := sdl.CreateWindow("Nate Editor",
|
||||
sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
|
||||
1280, 720,
|
||||
sdl.WINDOW_SHOWN)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer window.Destroy()
|
||||
window, err := sdl.CreateWindow("Nate Editor",
|
||||
sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
|
||||
1280, 720,
|
||||
sdl.WINDOW_SHOWN)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer window.Destroy()
|
||||
|
||||
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer renderer.Destroy()
|
||||
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer renderer.Destroy()
|
||||
|
||||
editor := &NateEditor{window: window, renderer: renderer, running: true, input_handler: &gui.InputHandler{}}
|
||||
editor.init()
|
||||
editor := &NateEditor{window: window, renderer: renderer, running: true, input_handler: &gui.InputHandler{}}
|
||||
editor.init()
|
||||
|
||||
timer := sdl.GetTicks()
|
||||
num_frames := 0
|
||||
timer := sdl.GetTicks()
|
||||
num_frames := 0
|
||||
|
||||
for editor.running {
|
||||
editor.update()
|
||||
editor.render()
|
||||
num_frames += 1
|
||||
for editor.running {
|
||||
editor.update()
|
||||
editor.render()
|
||||
num_frames += 1
|
||||
|
||||
if sdl.GetTicks() - timer > 1000 {
|
||||
timer = sdl.GetTicks()
|
||||
if (PRINT_FPS) {
|
||||
fmt.Println("frames: ", num_frames)
|
||||
}
|
||||
num_frames = 0
|
||||
}
|
||||
if sdl.GetTicks()-timer > 1000 {
|
||||
timer = sdl.GetTicks()
|
||||
if PRINT_FPS {
|
||||
fmt.Println("frames: ", num_frames)
|
||||
}
|
||||
num_frames = 0
|
||||
}
|
||||
|
||||
sdl.Delay(2)
|
||||
}
|
||||
sdl.Delay(2)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user