From 7c4706356b3a1270d66f812c39549cb6365eee70 Mon Sep 17 00:00:00 2001 From: Felix Angell Date: Sun, 15 Apr 2018 19:18:01 +0100 Subject: [PATCH] added mouse wheel scrolling + font face/size settings in toml configs --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 1 + cfg/config.go | 2 ++ cfg/macconfig.go | 36 ++++++++++++++++++------------------ cfg/windowsconfig.go | 12 +++++------- gui/buffer.go | 27 +++++++++++++++++++++------ gui/component.go | 6 ++++++ gui/input.go | 15 ++++++++++++++- main.go | 33 +++++++++++++++++++++++++++++---- 9 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f4f026a780ac215ee6daa6368c60f08a241af27b GIT binary patch literal 6148 zcmeHKO^(wr6n>r-CM^QGK$=-*gT$g#?Lak)5mM>2i%NhHL9hTclT1n*DUGWprGiq_ z4R-);z)?5>2jKwlJ==pwI&4ve{3QGH&-Y&9&yF1vk?79D7Ez6e93;ka71;&G{hT&z z$F#7Y;282Oh-H8phqGoCunKIu0{rck=!{;|Xb|VV-y`(g`l(C}qC_K_Q&eDmEwFa! z0S)Mw`t*oA>gU;FZ-YG{pCU@Af#|Zp{#amFX-Z?TZ_|)MU`CQ=1)m=Up9(#}yWFQ9 zJp?vrLSf#6c$}Yd40}-(^2|77Z6CwHwDW$^0l8&XjW$4ncYOJW4{j=*oE# zrYer7I~=FtR8DI9%jKQc-MV|wJXqD;<>6to?zY?Q)vDs`-E7?(oKD`pTfSd?_>7^z z1h#Bt*A1S-7ZggQM0n%Pl2|4qv@rV`rC2K5GsKjom<)+YXkc2B;_^{b{ik5((JgL` z0%id?SGMMj?2A|htODDj0Phbj5@W~UTBF)Jkf|#Gu#9G9D65};pvw)wj={A?w7`UR z1!`Aet{B4H9fWpAf5-6G8nrtK^<=~`PZs8eBFxo8xQb4qqtWJ80joe>fs(G)`26qx z{r;bKvNfxKRp7r;K$Lr4uZtm>vvp%|eAc>1r$}tfTWeG<$jou93VamrBPm0l%LQP^ U;94VEVD?8q$zU_9z&};s7yN3n$N&HU literal 0 HcmV?d00001 diff --git a/.gitignore b/.gitignore index be2c341..48ce5d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +glew.h phi-editor fmt.sh diff --git a/cfg/config.go b/cfg/config.go index e863e9f..5f78fc9 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -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 { diff --git a/cfg/macconfig.go b/cfg/macconfig.go index f8fe4ea..72933bd 100644 --- a/cfg/macconfig.go +++ b/cfg/macconfig.go @@ -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"` diff --git a/cfg/windowsconfig.go b/cfg/windowsconfig.go index e9edd11..d0a92a1 100644 --- a/cfg/windowsconfig.go +++ b/cfg/windowsconfig.go @@ -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"` diff --git a/gui/buffer.go b/gui/buffer.go index 5532dc1..3327093 100644 --- a/gui/buffer.go +++ b/gui/buffer.go @@ -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 diff --git a/gui/component.go b/gui/component.go index 20ad83c..7cb6d11 100644 --- a/gui/component.go +++ b/gui/component.go @@ -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-- diff --git a/gui/input.go b/gui/input.go index 0f0a9cd..61e8eea 100644 --- a/gui/input.go +++ b/gui/input.go @@ -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) + } +} diff --git a/main.go b/main.go index 223058b..10dc91e 100644 --- a/main.go +++ b/main.go @@ -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)