Clean text rendering in editor.

This commit is contained in:
James Grogan 2022-12-02 11:50:15 +00:00
parent 290b64e230
commit f16dd7c0d9
45 changed files with 59 additions and 60 deletions

View file

@ -0,0 +1,27 @@
#include "PlainTextDocument.h"
PlainTextDocument::PlainTextDocument()
: mContent()
{
}
std::unique_ptr<PlainTextDocument> PlainTextDocument::Create()
{
return std::make_unique<PlainTextDocument>();
}
std::string PlainTextDocument::GetContent() const
{
return mContent;
}
void PlainTextDocument::SetContent(const std::string& content)
{
mContent = content;
}
void PlainTextDocument::Clear()
{
mContent = "";
}

View file

@ -0,0 +1,24 @@
#pragma once
#include <string>
#include <memory>
class PlainTextDocument
{
std::string mContent;
public:
PlainTextDocument();
static std::unique_ptr<PlainTextDocument> Create();
std::string GetContent() const;
void SetContent(const std::string& content);
void Clear();
};
using PlainTextDocumentUPtr = std::unique_ptr<PlainTextDocument>;

View file

@ -0,0 +1,58 @@
#include "TextEditorController.h"
#include "File.h"
TextEditorController::TextEditorController()
: mModel(TextEditorModel::Create()),
mSavePath(),
mLoadPath()
{
}
std::unique_ptr<TextEditorController> TextEditorController::Create()
{
return std::make_unique<TextEditorController>();
}
void TextEditorController::SetContent(const std::string& content)
{
mModel->GetDocument()->SetContent(content);
}
std::string TextEditorController::GetContent() const
{
return mModel->GetDocument()->GetContent();
}
void TextEditorController::OnSave()
{
if(mSavePath.empty()) return;
File outfile(mSavePath);
outfile.open(File::AccessMode::Write);
outfile.writeText(mModel->GetDocument()->GetContent());
}
void TextEditorController::OnLoad()
{
if(mLoadPath.empty()) return;
File infile(mLoadPath);
infile.open(File::AccessMode::Read);
mModel->GetDocument()->SetContent(infile.readText());
}
void TextEditorController::SetSavePath(const std::filesystem::path& path)
{
mSavePath = path;
}
void TextEditorController::SetLoadPath(const std::filesystem::path& path)
{
mLoadPath = path;
}
void TextEditorController::OnClear()
{
mModel->GetDocument()->Clear();
}

View file

@ -0,0 +1,34 @@
#pragma once
#include <memory>
#include <filesystem>
#include "TextEditorModel.h"
class TextEditorController
{
public:
TextEditorController();
static std::unique_ptr<TextEditorController> Create();
void OnSave();
void OnClear();
void OnLoad();
std::string GetContent() const;
void SetContent(const std::string& content);
void SetSavePath(const std::filesystem::path& path);
void SetLoadPath(const std::filesystem::path& path);
private:
TextEditorModelUPtr mModel;
std::filesystem::path mSavePath;
std::filesystem::path mLoadPath;
};
using TextEditorControllerUPtr = std::unique_ptr<TextEditorController>;

View file

@ -0,0 +1,17 @@
#include "TextEditorModel.h"
TextEditorModel::TextEditorModel()
: mDocument(PlainTextDocument::Create())
{
}
std::unique_ptr<TextEditorModel> TextEditorModel::Create()
{
return std::make_unique<TextEditorModel>();
}
PlainTextDocument* TextEditorModel::GetDocument() const
{
return mDocument.get();
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "PlainTextDocument.h"
#include <memory>
class TextEditorModel
{
PlainTextDocumentUPtr mDocument;
public:
TextEditorModel();
static std::unique_ptr<TextEditorModel> Create();
PlainTextDocument* GetDocument() const;
};
using TextEditorModelUPtr = std::unique_ptr<TextEditorModel>;

View file

@ -0,0 +1,90 @@
#include "TextEditorView.h"
#include "HorizontalSpacer.h"
#include "VerticalSpacer.h"
#include "Theme.h"
#include "TextNode.h"
#include <iostream>
TextEditorView::TextEditorView()
: mTextBox(),
mController(TextEditorController::Create())
{
}
TextEditorController* TextEditorView::getController()
{
return mController.get();
}
void TextEditorView::initialize()
{
auto label = Label::Create();
label->setLabel("Text Editor");
label->setBackgroundColor(Theme::getBannerBackground());
label->setMargin(1);
auto textBox = TextBox::Create();
textBox->setName("Text Box");
mTextBox = textBox.get();
auto saveButton = Button::Create();
saveButton->setLabel("Save");
saveButton->setBackgroundColor(Theme::getButtonPrimaryBackground());
saveButton->setMargin(2);
auto onSave = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->SetContent(mTextBox->getContent());
mController->OnSave();
};
};
saveButton->setOnClickFunction(onSave);
auto clearButton = Button::Create();
clearButton->setLabel("Clear");
clearButton->setBackgroundColor(Theme::getButtonPrimaryBackground());
clearButton->setMargin(2);
auto onClear = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->OnClear();
mTextBox->setContent("");
};
};
clearButton->setOnClickFunction(onClear);
auto loadButton = Button::Create();
loadButton->setLabel("Load");
loadButton->setBackgroundColor(Theme::getButtonPrimaryBackground());
loadButton->setMargin(2);
auto onLoad = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->OnLoad();
mTextBox->setContent(mController->GetContent());
};
};
loadButton->setOnClickFunction(onLoad);
auto buttonSpacer = VerticalSpacer::Create();
buttonSpacer->AddWidgetWithScale(std::move(saveButton), 1);
buttonSpacer->AddWidgetWithScale(std::move(clearButton), 1);
buttonSpacer->AddWidgetWithScale(std::move(loadButton), 1);
auto hSpacer = HorizontalSpacer::Create();
hSpacer->addWidgetWithScale(std::move(label), 1);
hSpacer->addWidgetWithScale(std::move(textBox), 14);
hSpacer->addWidgetWithScale(std::move(buttonSpacer), 1);
addWidget(std::move(hSpacer));
}
std::unique_ptr<TextEditorView> TextEditorView::Create()
{
return std::make_unique<TextEditorView>();
}

View file

@ -0,0 +1,27 @@
#pragma once
#include "Widget.h"
#include "Button.h"
#include "Label.h"
#include "TextBox.h"
#include "TextEditorController.h"
#include <functional>
class TextEditorView : public Widget
{
public:
TextEditorView();
static std::unique_ptr<TextEditorView> Create();
TextEditorController* getController();
void initialize();
private:
TextBox* mTextBox;
TextEditorControllerUPtr mController;
};
using TextEditorViewUPtr = std::unique_ptr<TextEditorView>;