Move the Editor to a C++ class

This commit is contained in:
Curle 2021-07-15 06:26:14 +01:00
parent 8774b20356
commit af606752b2
3 changed files with 80 additions and 92 deletions

View File

@ -5,10 +5,6 @@
*** Chroma *** *** Chroma ***
***********************/ ***********************/
#ifdef __cplusplus
extern "C" {
#endif
/** /**
* This file contains most of the symbols required to start and run the Chroma Editor. * This file contains most of the symbols required to start and run the Chroma Editor.
* *
@ -33,40 +29,48 @@ extern "C" {
* - Drop shadow #000000. * - Drop shadow #000000.
*/ */
// Stores information about a single line.
struct EditorLine {
size_t Line;
size_t Length;
char* Text;
};
// Function pointer for menu-clicked callback. // Function pointer for menu-clicked callback.
typedef void (*MenuCallback)(void); typedef void (*MenuCallback)(void);
// A single item in the menu. class Editor
struct MenuItem { {
public:
// Stores information about a single line.
struct EditorLine
{
size_t Line;
size_t Length;
char *Text;
};
// A single item in the menu.
struct MenuItem
{
size_t ParentID; // 0 for the root menu. size_t ParentID; // 0 for the root menu.
size_t ID; size_t ID;
MenuCallback Callback; MenuCallback Callback;
}; };
// The full menu that should be rendered. Can be nested. // The full menu that should be rendered. Can be nested.
typedef struct { typedef struct
{
size_t MenuSize; size_t MenuSize;
struct MenuItem* Menu; struct MenuItem *Menu;
size_t ActiveMenuItem; size_t ActiveMenuItem;
} EditorMenu; } EditorMenu;
struct EditorMessage { struct EditorMessage
{
size_t TextLength; size_t TextLength;
char* Text; char *Text;
size_t MessageWidth; size_t MessageWidth;
size_t MessageHeight; size_t MessageHeight;
}; };
// Provides all the context for rendering the editor. // Provides all the context for rendering the editor.
struct EditorLayout { struct EditorLayout
{
size_t ScreenWidth; size_t ScreenWidth;
size_t ScreenHeight; size_t ScreenHeight;
@ -80,48 +84,40 @@ struct EditorLayout {
size_t TextBoxHeight; size_t TextBoxHeight;
bool HasMessage; bool HasMessage;
struct EditorMessage* CurrentMessage; struct EditorMessage *CurrentMessage;
}; };
// Given the kernel's keyboard handler ID, so that it may restore it at a later point.
void StartEditor(int KernelCallbackID);
private:
// =========================== Drawing routines =========================== //
// Provides all the context for manipulating state.
typedef struct {
struct EditorLayout Layout; struct EditorLayout Layout;
size_t Length; // How many lines? size_t Length = 0; // How many lines?
size_t Size; // How many characters? size_t Size = 0; // How many characters?
size_t CurrentLine; size_t CurrentLine = 0;
size_t CurrentColumn; size_t CurrentColumn = 0;
struct EditorLine* Lines; struct EditorLine *Lines;
} EditorState; // Draw everything.
void DrawEditor();
void DrawHeader();
void DrawBackground();
void DrawTextArea();
void DrawTextLine();
void DrawText();
void DrawMenus();
void DrawBoxes();
void DrawBoxShadows();
// Given the kernel's keyboard handler ID, so that it may restore it at a later point. // =========================== State management ===========================
void StartEditor(int KernelCallbackID);
// =========================== Drawing routines =========================== // // Text editing.
void GetLine(size_t line);
// Draw everything. void SetLine(struct EditorLine *line);
void DrawEditor(EditorState* currentState); void AppendLine(struct EditorLine *line);
void AppendToLine(struct EditorLine *line, size_t textLength, char *text);
void DrawHeader(); void RemoveLine(size_t line);
void DrawBackground(); };
void DrawTextArea();
void DrawTextLine();
void DrawText();
void DrawMenus();
void DrawBoxes();
void DrawBoxShadows();
// =========================== State management ===========================
EditorState* GetState();
// Text editing.
void GetLine(EditorState* state, size_t line);
void SetLine(EditorState* state, struct EditorLine* line);
void AppendLine(EditorState* state, struct EditorLine* line);
void AppendToLine(EditorState* state, struct EditorLine* line, size_t textLength, char* text);
void RemoveLine(EditorState* state, size_t line);
#ifdef __cplusplus
}
#endif

View File

@ -13,10 +13,10 @@
*/ */
static KeyboardCallback KernelHandler; static KeyboardCallback KernelHandler;
void StartEditor(int callbackID) { void Editor::StartEditor(int callbackID) {
KernelHandler = KeyboardCallbacks[callbackID]; KernelHandler = KeyboardCallbacks[callbackID];
struct EditorLayout layout; EditorLayout layout;
layout.ScreenHeight = PrintInfo.screenHeight; layout.ScreenHeight = PrintInfo.screenHeight;
layout.ScreenWidth = PrintInfo.screenWidth; layout.ScreenWidth = PrintInfo.screenWidth;
layout.HeaderHeight = layout.ScreenHeight / 100 * 3; layout.HeaderHeight = layout.ScreenHeight / 100 * 3;

View File

@ -36,7 +36,6 @@ char* InternalBuffer;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
/** /**
* C++ code! Scary! * C++ code! Scary!
* This is a temporary measure to experiment with the Editor system. * This is a temporary measure to experiment with the Editor system.
@ -90,10 +89,6 @@ int Main(void) {
return 0; return 0;
} }
#ifdef __cplusplus
extern "C" {
#endif
void PrintPressedChar(KeyboardData data) { void PrintPressedChar(KeyboardData data) {
if(!KernelLoaded) return; if(!KernelLoaded) return;
@ -120,7 +115,8 @@ void TrackInternalBuffer(KeyboardData data) {
InternalBuffer[BufferLength] = '\0'; // Null-terminate to make checking easier InternalBuffer[BufferLength] = '\0'; // Null-terminate to make checking easier
if(strcmp(InternalBuffer, "editor")) { if(strcmp(InternalBuffer, "editor")) {
UninstallKBCallback(InternalBufferID); UninstallKBCallback(InternalBufferID);
StartEditor(CharPrinterCallbackID); Editor editor;
editor.StartEditor(CharPrinterCallbackID);
} else if(strcmp(InternalBuffer, "zero")) { } else if(strcmp(InternalBuffer, "zero")) {
int returnVal = sharp_entryPoint(); int returnVal = sharp_entryPoint();
SerialPrintf("Sharp returned %d\r\n", returnVal); SerialPrintf("Sharp returned %d\r\n", returnVal);
@ -147,7 +143,3 @@ void SomethingWentWrong(const char* Message) {
void Exit(int ExitCode) { void Exit(int ExitCode) {
SerialPrintf("Kernel stopped with code %x\r\n", ExitCode); SerialPrintf("Kernel stopped with code %x\r\n", ExitCode);
} }
#ifdef __cplusplus
}
#endif