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,31 +29,38 @@ extern "C" {
* - Drop shadow #000000. * - Drop shadow #000000.
*/ */
// Function pointer for menu-clicked callback.
typedef void (*MenuCallback)(void);
class Editor
{
public:
// Stores information about a single line. // Stores information about a single line.
struct EditorLine { struct EditorLine
{
size_t Line; size_t Line;
size_t Length; size_t Length;
char *Text; char *Text;
}; };
// Function pointer for menu-clicked callback.
typedef void (*MenuCallback)(void);
// A single item in the menu. // A single item in the menu.
struct MenuItem { 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;
@ -66,7 +69,8 @@ struct EditorMessage {
}; };
// 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;
@ -83,25 +87,21 @@ struct EditorLayout {
struct EditorMessage *CurrentMessage; struct EditorMessage *CurrentMessage;
}; };
// Provides all the context for manipulating state.
typedef struct {
struct EditorLayout Layout;
size_t Length; // How many lines?
size_t Size; // How many characters?
size_t CurrentLine;
size_t CurrentColumn;
struct EditorLine* Lines;
} EditorState;
// Given the kernel's keyboard handler ID, so that it may restore it at a later point. // Given the kernel's keyboard handler ID, so that it may restore it at a later point.
void StartEditor(int KernelCallbackID); void StartEditor(int KernelCallbackID);
private:
// =========================== Drawing routines =========================== // // =========================== Drawing routines =========================== //
struct EditorLayout Layout;
size_t Length = 0; // How many lines?
size_t Size = 0; // How many characters?
size_t CurrentLine = 0;
size_t CurrentColumn = 0;
struct EditorLine *Lines;
// Draw everything. // Draw everything.
void DrawEditor(EditorState* currentState); void DrawEditor();
void DrawHeader(); void DrawHeader();
void DrawBackground(); void DrawBackground();
@ -113,15 +113,11 @@ void DrawBoxes();
void DrawBoxShadows(); void DrawBoxShadows();
// =========================== State management =========================== // =========================== State management ===========================
EditorState* GetState();
// Text editing. // Text editing.
void GetLine(EditorState* state, size_t line); void GetLine(size_t line);
void SetLine(EditorState* state, struct EditorLine* line); void SetLine(struct EditorLine *line);
void AppendLine(EditorState* state, struct EditorLine* line); void AppendLine(struct EditorLine *line);
void AppendToLine(EditorState* state, struct EditorLine* line, size_t textLength, char* text); void AppendToLine(struct EditorLine *line, size_t textLength, char *text);
void RemoveLine(EditorState* state, size_t line); void RemoveLine(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