added mouse wheel scrolling + font face/size settings in toml configs

This commit is contained in:
Felix Angell 2018-04-15 19:18:01 +01:00
parent 483066ab6f
commit 7c4706356b
9 changed files with 96 additions and 36 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
glew.h
phi-editor
fmt.sh

View File

@ -112,6 +112,8 @@ type EditorConfig struct {
Match_Braces bool
Maintain_Indentation bool
Highlight_Line bool
Font_Face string
Font_Size int
}
func NewDefaultConfig() *TomlConfig {

View File

@ -7,13 +7,26 @@ tabs_are_spaces = true
match_braces = false
maintain_indentation = true
highlight_line = true
font_face = "Courier New Bold"
font_size = 20
[render]
aliased = true
accelerated = true
throttle_cpu_usage = true
always_render = false
always_render = true
[theme]
background = 0x002649
foreground = 0xf2f4f6
cursor = 0xf2f4f6
cursor_invert = 0x000000
[cursor]
flash_rate = 400
reset_delay = 400
draw = true
flash = true
[file_associations]
[file_associations.c]
@ -29,7 +42,7 @@ match = [
"type", "import", "package", "func", "struct",
"append", "delete", "make", "for", "if", "while",
"switch", "select", "chan", "else", "var", "const",
"iota"
"iota", "case"
]
[syntax.go.type]
@ -42,8 +55,8 @@ match = [
]
[syntax.go.comment]
colouring = 0xff00ff
pattern = "[\/]+.*"
colouring = 0x4b79fc
pattern = "[\\/]+.*"
[syntax.go.symbol]
colouring = 0xf0a400
@ -68,18 +81,6 @@ match = [
"goto", "static", "extern", "const", "typedef",
]
[theme]
background = 0x002649
foreground = 0xf2f4f6
cursor = 0xf2f4f6
cursor_invert = 0x000000
[cursor]
flash_rate = 400
reset_delay = 400
draw = true
flash = true
[commands]
[commands.save]
shortcut = "super+s"
@ -88,5 +89,4 @@ shortcut = "super+s"
shortcut = "super+w"
[commands.delete_line]
shortcut = "super+d"
`
shortcut = "super+d"`

View File

@ -12,8 +12,7 @@ highlight_line = true
aliased = true
accelerated = true
throttle_cpu_usage = true
always_render = false
always_render = true
[file_associations]
[file_associations.c]
@ -29,7 +28,7 @@ match = [
"type", "import", "package", "func", "struct",
"append", "delete", "make", "for", "if", "while",
"switch", "select", "chan", "else", "var", "const",
"iota"
"iota", "case"
]
[syntax.go.type]
@ -42,8 +41,8 @@ match = [
]
[syntax.go.comment]
colouring = 0xff00ff
pattern = "[\/]+.*"
colouring = 0x4b79fc
pattern = "[\\/]+.*"
[syntax.go.symbol]
colouring = 0xf0a400
@ -88,5 +87,4 @@ shortcut = "ctrl+s"
shortcut = "ctrl+w"
[commands.delete_line]
shortcut = "ctrl+d"
`
shortcut = "ctrl+d"`

View File

@ -357,13 +357,16 @@ func (b *Buffer) swapLineDown() bool {
}
func (b *Buffer) scrollUp() {
// TODO move the cursor down 45 lines
// IF the buffer exceeds the window size.
lineScrollAmount := 10
b.cam.y -= lineScrollAmount
for i := 0; i < lineScrollAmount; i++ {
b.moveUp()
if b.cam.y > 0 {
// TODO move the cursor down 45 lines
// IF the buffer exceeds the window size.
lineScrollAmount := 10
b.cam.y -= lineScrollAmount
for i := 0; i < lineScrollAmount; i++ {
b.moveUp()
}
}
}
func (b *Buffer) scrollDown() {
@ -625,6 +628,18 @@ func (b *Buffer) makeTab() string {
return string(blah)
}
func (b *Buffer) HandleEvent(evt strife.StrifeEvent) {
switch event := evt.(type) {
case *strife.MouseWheelEvent:
if event.Y > 0 {
b.scrollDown()
}
if event.Y < 0 {
b.scrollUp()
}
}
}
func (b *Buffer) OnUpdate() bool {
b.HasFocus = true

View File

@ -18,6 +18,8 @@ type Component interface {
AddComponent(c Component)
GetComponents() []Component
HandleEvent(evt strife.StrifeEvent)
GetInputHandler() *InputHandler
SetInputHandler(h *InputHandler)
}
@ -30,6 +32,10 @@ type BaseComponent struct {
inputHandler *InputHandler
}
func (b *BaseComponent) HandleEvent(evt strife.StrifeEvent) {
// NOP
}
func (b *BaseComponent) DeleteComponent(index int) {
b.components[index] = nil
b.numComponents--

View File

@ -1,7 +1,20 @@
package gui
import "github.com/veandco/go-sdl2/sdl"
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)
}
}

33
main.go
View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"path"
"runtime"
"time"
@ -23,6 +24,12 @@ type PhiEditor struct {
defaultFont *strife.Font
}
func (n *PhiEditor) handleEvent(evt strife.StrifeEvent) {
for _, comp := range n.GetComponents() {
gui.HandleEvent(comp, evt)
}
}
func (n *PhiEditor) init(cfg *cfg.TomlConfig) {
mainView := gui.NewView(1280, 720, cfg)
@ -46,7 +53,22 @@ func (n *PhiEditor) init(cfg *cfg.TomlConfig) {
n.AddComponent(mainView)
font, err := strife.LoadFont("./res/firacode.ttf", 20)
// TODO put me somewhere else:
// also improve the font loading code
var fontFolder string
switch runtime.GOOS {
case "windows":
fontFolder = path.Join(os.Getenv("%WINDIR%"), "fonts")
case "darwin":
fontFolder = "/Library/Fonts/"
case "linux":
fontFolder = "/usr/share/fonts/"
}
fontPath := path.Join(fontFolder, cfg.Editor.Font_Face) + ".ttf"
font, err := strife.LoadFont(fontPath, cfg.Editor.Font_Size)
if err != nil {
panic(err)
}
@ -91,6 +113,8 @@ func main() {
switch evt.(type) {
case *strife.CloseEvent:
window.Close()
default:
editor.handleEvent(evt)
}
})
@ -111,10 +135,11 @@ func main() {
icon, err := strife.LoadImage("./res/icons/icon" + size + ".png")
if err != nil {
panic(err)
log.Println("Failed to load icon ", err.Error())
} else {
window.SetIconImage(icon)
defer icon.Destroy()
}
window.SetIconImage(icon)
defer icon.Destroy()
}
editor.init(&config)