Refactored text window bounds, added initial GUI art

This commit is contained in:
Bobby Lucero 2024-10-03 23:25:22 -04:00
parent dde45cb306
commit efc9b0fe5e
8 changed files with 80 additions and 27 deletions

View File

@ -47,7 +47,9 @@ add_executable(Pycron src/main.cpp
src/Graphics/PycronImage.h src/Graphics/PycronImage.h
src/Graphics/PycronImage.cpp src/Graphics/PycronImage.cpp
src/States/Editor/PythonTokenizer.cpp src/States/Editor/PythonTokenizer.cpp
src/States/Editor/PythonTokenizer.h) src/States/Editor/PythonTokenizer.h
src/Graphics/Rectangle.cpp
src/Graphics/Rectangle.h)
add_subdirectory(dependencies/pocketpy) add_subdirectory(dependencies/pocketpy)
# Declaring our executable # Declaring our executable

BIN
resources/EditorFrame.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
resources/GUITest.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

BIN
resources/pico8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@ -0,0 +1,9 @@
//
// Created by Bobby Lucero on 10/3/24.
//
#include "Rectangle.h"
pycron::Rectangle::Rectangle(int x, int y, int width, int height) : x(x), y(y), width(width), height(height){
}

13
src/Graphics/Rectangle.h Normal file
View File

@ -0,0 +1,13 @@
//
// Created by Bobby Lucero on 10/3/24.
//
#pragma once
namespace pycron{
class Rectangle {
public:
int x, y;
int width, height;
Rectangle(int x, int y, int width, int height);
};
}

View File

@ -5,10 +5,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include "EditorState.h"
#include "../../Graphics/PycronImage.h"
#include "../../Pycron.h"
#include <raylib.h> #include <raylib.h>
#include "EditorState.h"
EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){
@ -18,6 +17,8 @@ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphic
std::string randomSource = Pycron::loadFileToString("../python/triangles.py"); std::string randomSource = Pycron::loadFileToString("../python/triangles.py");
m_editorFrame = m_graphics->loadImage("../resources/EditorFrame.png");
m_baseBackgroundColor = 56; m_baseBackgroundColor = 56;
m_shadowColor = 28; m_shadowColor = 28;
m_baseTextColor = 42; m_baseTextColor = 42;
@ -41,17 +42,31 @@ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphic
m_cursorX = 0; m_cursorX = 0;
m_cursorY = 0; m_cursorY = 0;
m_charWidth = m_leftLetterSpacing + m_graphics->GetCurrentFontWidth() + m_rightLetterSpacing; // Final size of char with spacing. If the literal width of the font is n, the final width is n + spacing.
m_charHeight = m_topLetterSpacing + m_graphics->GetCurrentFontHeight() + m_bottomLetterSpacing;
// Hardcoded text editor element positions based on interface graphics
m_textWindowXOffset = 26;
m_textWindowYOffset = 20;
m_textWindowWidth = 330;
m_textWindowHeight = 168;
m_textBounds = new pycron::Rectangle(m_textWindowXOffset, m_textWindowYOffset, m_textWindowWidth, m_textWindowHeight);
m_scrollX = 0; m_scrollX = 0;
m_scrollY = 0; m_scrollY = 0;
m_cursorBlinkTimer = 0; m_cursorBlinkTimer = 0;
m_cursorBlinkInterval = 0.5; m_cursorBlinkInterval = 0.5;
m_charWidth = m_leftLetterSpacing + m_graphics->GetCurrentFontWidth() + m_rightLetterSpacing; // Final size of char with spacing. If the literal width of the font is n, the final width is n + spacing.
m_charHeight = m_topLetterSpacing + m_graphics->GetCurrentFontHeight() + m_bottomLetterSpacing;
m_width = (int)(m_graphics->m_screenWidth / m_charWidth);
m_height = (int)(m_graphics->m_screenHeight / m_charHeight); std::cout << (int)m_charWidth << "!\n";
m_width = (int)(m_textBounds->width / m_charWidth);
std::cout << m_textBounds->width << " : " << (int)m_charWidth << " = " << m_width;
m_height = (int)(m_textBounds->height / m_charHeight);
m_characterBuffer = std::vector<char>(m_width * m_height); m_characterBuffer = std::vector<char>(m_width * m_height);
m_foregroundIndexBuffer = std::vector<uint8_t>(m_width * m_height); m_foregroundIndexBuffer = std::vector<uint8_t>(m_width * m_height);
@ -69,10 +84,11 @@ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphic
EditorState::~EditorState() { EditorState::~EditorState() {
delete m_pythonTokenizer; delete m_pythonTokenizer;
delete m_editorFrame;
delete m_textBounds;
} }
void EditorState::Draw() { void EditorState::Draw() {
// Set text and color buffers if needed // Set text and color buffers if needed
if(m_dirty){ if(m_dirty){
Clear(); Clear();
@ -87,12 +103,6 @@ void EditorState::Draw() {
if(index > m_text.size() - 1) break; 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 // Text handling
auto tokens = m_pythonTokenizer->tokenizeLine(m_text[index]); auto tokens = m_pythonTokenizer->tokenizeLine(m_text[index]);
@ -119,43 +129,48 @@ void EditorState::Draw() {
}else if(type == TokenType::Unknown){ }else if(type == TokenType::Unknown){
color = m_baseTextColor; color = m_baseTextColor;
} }
Text(tokens[j].value, size + currentPos, i, color, m_baseBackgroundColor); Text(tokens[j].value, currentPos, i, color, m_baseBackgroundColor);
currentPos += tokens[j].value.size(); currentPos += (int)tokens[j].value.size();
} }
} }
} }
m_graphics->Clear(0); m_graphics->Clear(57);
// draw text with info from color buffers // draw text with info from color buffers
for (int i = 0; i < m_characterBuffer.size(); ++i) { for (int i = 0; i < m_characterBuffer.size(); ++i) {
int x = (i % m_width) * m_charWidth; int x = (i % m_width) * m_charWidth + m_textBounds->x;
int y = (i / m_width) * m_charHeight; int y = (i / m_width) * m_charHeight + m_textBounds->y;
m_graphics->Rect(x, y, m_charWidth, m_charHeight, m_backgroundIndexBuffer[i]); m_graphics->Rect(x, y, m_charWidth, m_charHeight, m_backgroundIndexBuffer[i]);
if(m_drawShadows) m_graphics->Char(m_characterBuffer[i], x + m_leftLetterSpacing + 1, y + m_topLetterSpacing + 1, m_shadowColor); if(m_drawShadows) m_graphics->Char(m_characterBuffer[i], x + m_leftLetterSpacing + 1, y + m_topLetterSpacing + 1, m_shadowColor);
m_graphics->Char(m_characterBuffer[i], x + m_leftLetterSpacing, y + m_topLetterSpacing, m_foregroundIndexBuffer[i]); m_graphics->Char(m_characterBuffer[i], x + m_leftLetterSpacing, y + m_topLetterSpacing, m_foregroundIndexBuffer[i]);
} }
// Draw the cursor
if(m_cursorVisible) { if(m_cursorVisible) {
int x = (m_cursorX + 2) * m_charWidth; int x = m_cursorX * m_charWidth + m_textBounds->x;
int y = m_cursorY * m_charHeight; int y = m_cursorY * m_charHeight + m_textBounds->y;
int index = m_cursorY * m_width + (m_cursorX + 2); int index = m_cursorY * m_width + m_cursorX;
if(m_drawShadows) m_graphics->Rect(x, y, m_charWidth + 1, m_charHeight + 1, m_shadowColor); if(m_drawShadows) m_graphics->Rect(x, y, m_charWidth + 1, m_charHeight + 1, m_shadowColor);
m_graphics->Rect(x - 1, y - 1, m_charWidth + 1, m_charHeight + 1, m_foregroundIndexBuffer[index]); m_graphics->Rect(x - 1, y - 1, m_charWidth + 1, m_charHeight + 1, m_foregroundIndexBuffer[index]);
// m_graphics->Line(x - 1, y, x - 1, y + m_charHeight - 1, 63);
std::string s(1, m_characterBuffer[index]); std::string s(1, m_characterBuffer[index]);
m_graphics->Text(s, x, y, m_backgroundIndexBuffer[index]); m_graphics->Text(s, x, y, m_backgroundIndexBuffer[index]);
} }
// Cursor blink // Cursor blink timer TODO: probably move to an update function
m_cursorBlinkTimer += GetFrameTime(); m_cursorBlinkTimer += GetFrameTime();
if(m_cursorBlinkTimer > m_cursorBlinkInterval){ if(m_cursorBlinkTimer > m_cursorBlinkInterval){
m_cursorVisible = !m_cursorVisible; m_cursorVisible = !m_cursorVisible;
m_cursorBlinkTimer = 0.0; m_cursorBlinkTimer = 0.0;
} }
// Editor frame image
m_graphics->Img(m_editorFrame, 0, 0);
//m_graphics->RectBorder(m_textBounds->x, m_textBounds->y, m_textBounds->width, m_textBounds->height, 23);
} }
void EditorState::OnEnter() { void EditorState::OnEnter() {
@ -170,7 +185,7 @@ void EditorState::OnKeyPressed(int key) {
if(key == KEY_LEFT && m_cursorX > 0){ if(key == KEY_LEFT && m_cursorX > 0){
m_cursorX--; m_cursorX--;
} }
if(key == KEY_RIGHT && m_cursorX < m_width - 1 - 2){ if(key == KEY_RIGHT && m_cursorX < m_width - 1){
m_cursorX++; m_cursorX++;
} }
if(key == KEY_UP){ if(key == KEY_UP){

View File

@ -3,7 +3,9 @@
#include <regex> #include <regex>
#include "PythonTokenizer.h" #include "PythonTokenizer.h"
#include "../../State.h" #include "../../State.h"
#include "../../Graphics/PycronImage.h"
#include "../../Pycron.h"
#include "../../Graphics/Rectangle.h"
class PycronImage; class PycronImage;
@ -24,6 +26,9 @@ private:
Graphics* m_graphics; Graphics* m_graphics;
PythonTokenizer* m_pythonTokenizer; PythonTokenizer* m_pythonTokenizer;
// Editor images
PycronImage* m_editorFrame;
// Size of the character in pixels // Size of the character in pixels
uint8_t m_charWidth, m_charHeight; uint8_t m_charWidth, m_charHeight;
@ -62,6 +67,15 @@ private:
float_t m_cursorBlinkTimer; float_t m_cursorBlinkTimer;
float_t m_cursorBlinkInterval; float_t m_cursorBlinkInterval;
int m_textWindowXOffset;
int m_textWindowYOffset;
int m_textWindowWidth;
int m_textWindowHeight;
pycron::Rectangle* m_textBounds;
int m_scrollX; int m_scrollX;
int m_scrollY; int m_scrollY;