From 0a35d9ab22a392c80e79929dce829e480bc759bc Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Tue, 1 Oct 2024 22:09:37 -0400 Subject: [PATCH] Added vertical scrolling --- python/syntaxTest.py | 19 ------------ src/States/Editor/EditorState.cpp | 50 ++++++++++++++++++++++++------- src/States/Editor/EditorState.h | 3 ++ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/python/syntaxTest.py b/python/syntaxTest.py index ca7c106..313df4c 100644 --- a/python/syntaxTest.py +++ b/python/syntaxTest.py @@ -1,23 +1,4 @@ -#asdlkjasd -test #this is a test - -print("test") - -''' -test -''' - -x is .10 - -{"False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", - "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", - "nonlocal", "not", "or", "pass", "raise", "return", "try", "while", "with", "yield", "self"}; - -{False, None, True, and, as, assert, async, await, break, class, continue, def, - del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, - nonlocal, not, or, pass, raise, return, try, while, with, yield, self}; - i in range(100): print(i) diff --git a/src/States/Editor/EditorState.cpp b/src/States/Editor/EditorState.cpp index a92322f..08b162c 100644 --- a/src/States/Editor/EditorState.cpp +++ b/src/States/Editor/EditorState.cpp @@ -4,7 +4,7 @@ #include #include - +#include #include "EditorState.h" #include "../../Graphics/PycronImage.h" #include "../../Pycron.h" @@ -16,7 +16,7 @@ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphic Token a(TokenType::Keyword, "Test"); - std::string randomSource = Pycron::loadFileToString("../python/main.py"); + std::string randomSource = Pycron::loadFileToString("../python/triangles.py"); m_baseBackgroundColor = 56; m_shadowColor = 28; @@ -41,6 +41,9 @@ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphic m_cursorX = 0; m_cursorY = 0; + m_scrollX = 0; + m_scrollY = 0; + m_cursorBlinkTimer = 0; m_cursorBlinkInterval = 0.5; @@ -62,8 +65,6 @@ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphic LoadStringToBuffer(randomSource); - auto tokens = m_pythonTokenizer->tokenizeLine("for i in range(100):"); - } EditorState::~EditorState() { @@ -76,18 +77,24 @@ void EditorState::Draw() { if(m_dirty){ Clear(); m_dirty = false; - for (int i = 0; i < m_text.size(); ++i) { + for (int i = 0; i < m_height; ++i) { + + // if(i > m_text.size() - 1) break; // Line numbers TODO: maybe not have this as part of the buffer, instead as a custom bar. (Allows for more custom functionality such as bookmarks) // std::string lineNumber = std::to_string(std::abs(m_cursorY - i)); // if(i == m_cursorY) lineNumber = std::to_string(m_cursorY); - std::string lineNumber = std::to_string(i); + int index = i + m_scrollY; + + if(index > m_text.size() - 1) break; + + std::string lineNumber = std::to_string(index); int size = 2; int diff = size - (int)lineNumber.size(); if(diff > 0) lineNumber = std::string(diff, ' ') + lineNumber; Text(lineNumber, 0, i, i == m_cursorY ? m_lineNumberTextColor : m_commentTextColor, m_lineNumberBackgroundColor); // Text handling - auto tokens = m_pythonTokenizer->tokenizeLine(m_text[i]); + auto tokens = m_pythonTokenizer->tokenizeLine(m_text[index]); int currentPos = 0; for (int j = 0; j < tokens.size(); ++j) { @@ -166,13 +173,34 @@ void EditorState::OnKeyPressed(int key) { if(key == KEY_RIGHT && m_cursorX < m_width - 1 - 2){ m_cursorX++; } - if(key == KEY_UP && m_cursorY > 0){ - m_cursorY--; + if(key == KEY_UP){ + if( m_cursorY > 0) + { + m_cursorY--; + } + else + { + if(m_scrollY > 0) + { + m_scrollY--; + } + } } - if(key == KEY_DOWN && m_cursorY < m_height - 1){ - m_cursorY++; + + if(key == KEY_DOWN ) + { + if(m_cursorY < std::min(m_height - 1, static_cast(m_text.size()) - 1)) + { + m_cursorY++; + } + else + { + if(m_scrollY + m_height < m_text.size()) + m_scrollY++; + } } + m_cursorVisible = true; m_cursorBlinkTimer = 0; diff --git a/src/States/Editor/EditorState.h b/src/States/Editor/EditorState.h index 5312b73..ab0b1d3 100644 --- a/src/States/Editor/EditorState.h +++ b/src/States/Editor/EditorState.h @@ -62,6 +62,9 @@ private: float_t m_cursorBlinkTimer; float_t m_cursorBlinkInterval; + int m_scrollX; + int m_scrollY; + bool m_dirty; bool m_drawShadows;