stuff-from-scratch/apps/sample-gui/text_editor/TextEditorView.cpp

84 lines
2.5 KiB
C++
Raw Normal View History

2020-06-27 09:47:30 +00:00
#include "HorizontalSpacer.h"
#include "TextEditorView.h"
#include "VerticalSpacer.h"
#include <iostream>
TextEditorView::TextEditorView()
: mTextBox(),
mController(TextEditorController::Create())
{
}
TextEditorController* TextEditorView::GetController()
{
return mController.get();
}
void TextEditorView::Initialize()
{
auto label = Label::Create();
2022-11-11 14:22:31 +00:00
label->setLabel("Text Editor");
2022-11-13 17:02:09 +00:00
label->setBackgroundColor(Color(181, 189, 200));
2022-11-11 14:22:31 +00:00
label->setMargin(1);
2020-06-27 09:47:30 +00:00
auto textBox = TextBox::Create();
mTextBox = textBox.get();
auto saveButton = Button::Create();
2022-11-11 14:22:31 +00:00
saveButton->setLabel("Save");
2022-11-13 17:02:09 +00:00
saveButton->setBackgroundColor(Color(200, 200, 200));
2022-11-11 14:22:31 +00:00
saveButton->setMargin(2);
2020-06-27 09:47:30 +00:00
auto onSave = [this](Widget* self){
if(this && mController && mTextBox)
{
2022-11-13 17:02:09 +00:00
mController->SetContent(mTextBox->getContent());
2020-06-27 09:47:30 +00:00
mController->OnSave();
};
};
2022-11-11 14:22:31 +00:00
saveButton->setOnClickFunction(onSave);
2020-06-27 09:47:30 +00:00
auto clearButton = Button::Create();
2022-11-11 14:22:31 +00:00
clearButton->setLabel("Clear");
2022-11-13 17:02:09 +00:00
clearButton->setBackgroundColor(Color(200, 200, 200));
2022-11-11 14:22:31 +00:00
clearButton->setMargin(2);
2020-06-27 09:47:30 +00:00
auto onClear = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->OnClear();
2022-11-13 17:02:09 +00:00
mTextBox->setContent("");
2020-06-27 09:47:30 +00:00
};
};
2022-11-11 14:22:31 +00:00
clearButton->setOnClickFunction(onClear);
2020-06-27 09:47:30 +00:00
auto loadButton = Button::Create();
2022-11-11 14:22:31 +00:00
loadButton->setLabel("Load");
2022-11-13 17:02:09 +00:00
loadButton->setBackgroundColor(Color(200, 200, 200));
2022-11-11 14:22:31 +00:00
loadButton->setMargin(2);
2020-06-27 09:47:30 +00:00
auto onLoad = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->OnLoad();
2022-11-13 17:02:09 +00:00
mTextBox->setContent(mController->GetContent());
2020-06-27 09:47:30 +00:00
};
};
2022-11-11 14:22:31 +00:00
loadButton->setOnClickFunction(onLoad);
2020-06-27 09:47:30 +00:00
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();
2022-11-11 14:22:31 +00:00
hSpacer->addWidgetWithScale(std::move(label), 1);
hSpacer->addWidgetWithScale(std::move(textBox), 14);
hSpacer->addWidgetWithScale(std::move(buttonSpacer), 1);
2020-06-27 09:47:30 +00:00
2022-11-11 14:22:31 +00:00
addWidget(std::move(hSpacer));
2020-06-27 09:47:30 +00:00
}
std::unique_ptr<TextEditorView> TextEditorView::Create()
{
return std::make_unique<TextEditorView>();
}