From 8630fa3fd30e91dd1360d03b3adc1c6fe1da8691 Mon Sep 17 00:00:00 2001 From: Matthieu-Riou Date: Fri, 28 Nov 2014 17:23:35 +0100 Subject: [PATCH] Now we have a cursor (only one) --- .../scala/io/atal/butterfly/Interface.scala | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/scala/io/atal/butterfly/Interface.scala b/src/main/scala/io/atal/butterfly/Interface.scala index d45b296..87d02bc 100644 --- a/src/main/scala/io/atal/butterfly/Interface.scala +++ b/src/main/scala/io/atal/butterfly/Interface.scala @@ -16,10 +16,13 @@ object HelloWorld extends SimpleSwingApplication { var current: Editor = editorManager.currentEditor.get var isSpec = false - object editor extends Label { + object editor extends EditorPane { text = current.buffer.content preferredSize = new Dimension(1000,500) + editable = false + caret.visible = true + focusable = true requestFocus @@ -38,6 +41,16 @@ object HelloWorld extends SimpleSwingApplication { updateLabel } + case KeyPressed(_, Key.Left, _, _) => { + isSpec = true + current.moveCursorsLeft() + } + + case KeyPressed(_, Key.Right, _, _) => { + isSpec = true + current.moveCursorsRight() + } + case KeyPressed(_, x, _, _) => isSpec = false //Allow for non-specified KeyPressed (like Key.BackSpace) to be match with KeyTyped. It's ugly. Better way ? case KeyTyped(_, y, _, _) => { @@ -56,10 +69,11 @@ object HelloWorld extends SimpleSwingApplication { } - def updateLabel: Unit = editor.text = bufferToLabel(current.buffer.content) + def updateLabel: Unit = { + editor.text = current.buffer.content + editor.caret.position = current.getIndexPosition(current.cursors.head) + } } - - def bufferToLabel(s: String) : String = return "" + s.replaceAll("\n", "
") + "" }