Simple drawing example.
This commit is contained in:
parent
d7fe11913f
commit
f0091f9e04
27 changed files with 450 additions and 68 deletions
|
@ -13,12 +13,13 @@ list(APPEND client_HEADERS
|
|||
image_editor/ImageViewWidget.h
|
||||
canvas/CanvasView.h
|
||||
canvas/CanvasController.h
|
||||
canvas/CanvasDrawingArea.h
|
||||
canvas/CanvasCommandSelectorView.h
|
||||
mesh_viewer/MeshViewerView.h
|
||||
mesh_viewer/MeshViewerController.h
|
||||
mesh_viewer/MeshViewerCanvas.h
|
||||
web_client/WebClientView.h)
|
||||
|
||||
|
||||
list(APPEND client_LIB_INCLUDES
|
||||
text_editor/TextEditorView.cpp
|
||||
text_editor/TextEditorModel.cpp
|
||||
|
@ -32,6 +33,8 @@ list(APPEND client_LIB_INCLUDES
|
|||
mesh_viewer/MeshViewerCanvas.cpp
|
||||
canvas/CanvasView.cpp
|
||||
canvas/CanvasController.cpp
|
||||
canvas/CanvasDrawingArea.cpp
|
||||
canvas/CanvasCommandSelectorView.cpp
|
||||
web_client/WebClientView.cpp
|
||||
NotesTk.cpp)
|
||||
|
||||
|
|
42
apps/notes_tk/canvas/CanvasCommandSelectorView.cpp
Normal file
42
apps/notes_tk/canvas/CanvasCommandSelectorView.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "CanvasCommandSelectorView.h"
|
||||
|
||||
#include "HorizontalSpacer.h"
|
||||
#include "Button.h"
|
||||
#include "Theme.h"
|
||||
|
||||
CanvasCommandSelectorView::CanvasCommandSelectorView()
|
||||
: Widget()
|
||||
{
|
||||
auto circle_button = Button::Create();
|
||||
|
||||
auto on_circle_click = [this](Widget* self){
|
||||
onCommandSelected(CanvasDrawCommand::CIRCLE);
|
||||
};
|
||||
circle_button->setLabel("Circle");
|
||||
circle_button->setBackgroundColor(Theme::getButtonPrimaryBackground());
|
||||
circle_button->setMargin(2);
|
||||
circle_button->setOnClickFunction(on_circle_click);
|
||||
|
||||
auto on_line_click = [this](Widget* self){
|
||||
onCommandSelected(CanvasDrawCommand::LINE);
|
||||
};
|
||||
auto line_button = Button::Create();
|
||||
line_button->setLabel("Line");
|
||||
line_button->setBackgroundColor(Theme::getButtonPrimaryBackground());
|
||||
line_button->setMargin(2);
|
||||
line_button->setOnClickFunction(on_line_click);
|
||||
|
||||
auto hspacer = HorizontalSpacer::Create();
|
||||
hspacer->addWidgetWithScale(std::move(circle_button), 1);
|
||||
hspacer->addWidgetWithScale(std::move(line_button), 1);
|
||||
|
||||
addWidget(std::move(hspacer));
|
||||
}
|
||||
|
||||
void CanvasCommandSelectorView::onCommandSelected(CanvasDrawCommand command)
|
||||
{
|
||||
if(mCommandSelectedCallback)
|
||||
{
|
||||
mCommandSelectedCallback(command);
|
||||
}
|
||||
}
|
24
apps/notes_tk/canvas/CanvasCommandSelectorView.h
Normal file
24
apps/notes_tk/canvas/CanvasCommandSelectorView.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include "Widget.h"
|
||||
#include "CanvasElements.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
class CanvasCommandSelectorView : public Widget
|
||||
{
|
||||
public:
|
||||
|
||||
using onCommandSelectedFunc = std::function<void(CanvasDrawCommand command)>;
|
||||
CanvasCommandSelectorView();
|
||||
|
||||
void setCommandSelectedCallback(onCommandSelectedFunc func)
|
||||
{
|
||||
mCommandSelectedCallback = func;
|
||||
}
|
||||
|
||||
void onCommandSelected(CanvasDrawCommand command);
|
||||
|
||||
private:
|
||||
onCommandSelectedFunc mCommandSelectedCallback{nullptr};
|
||||
};
|
54
apps/notes_tk/canvas/CanvasDrawingArea.cpp
Normal file
54
apps/notes_tk/canvas/CanvasDrawingArea.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "CanvasDrawingArea.h"
|
||||
|
||||
#include "MouseEvent.h"
|
||||
|
||||
#include "GeometryNode.h"
|
||||
#include "TransformNode.h"
|
||||
#include "CircleNode.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
CanvasDrawingArea::~CanvasDrawingArea()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CanvasDrawingArea::addShapeAt(unsigned x, unsigned y)
|
||||
{
|
||||
if (mActiveDrawingCommand == CanvasDrawCommand::CIRCLE)
|
||||
{
|
||||
auto circle = std::make_unique<CircleNode>(DiscretePoint(x, y), 5);
|
||||
circle->setFillColor(Color(255, 0, 0));
|
||||
circle->setName("CanvasDrawingArea_CircleNode");
|
||||
|
||||
mRootNode->addChild(circle.get());
|
||||
|
||||
mSceneNodes.push_back(std::move(circle));
|
||||
mContentDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool CanvasDrawingArea::isDirty() const
|
||||
{
|
||||
return Widget::isDirty() || mContentDirty;
|
||||
}
|
||||
|
||||
void CanvasDrawingArea::doPaint(const PaintEvent* event)
|
||||
{
|
||||
mContentDirty = false;
|
||||
}
|
||||
|
||||
void CanvasDrawingArea::onMyMouseEvent(const MouseEvent* event)
|
||||
{
|
||||
if(event->GetAction() == MouseEvent::Action::Pressed)
|
||||
{
|
||||
|
||||
}
|
||||
else if(event->GetAction() == MouseEvent::Action::Released)
|
||||
{
|
||||
auto client_loc = event->GetClientLocation();
|
||||
auto screen_loc = event->GetScreenLocation();
|
||||
|
||||
addShapeAt(client_loc.GetX(), client_loc.GetY());
|
||||
}
|
||||
}
|
29
apps/notes_tk/canvas/CanvasDrawingArea.h
Normal file
29
apps/notes_tk/canvas/CanvasDrawingArea.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include "Widget.h"
|
||||
#include "CanvasElements.h"
|
||||
|
||||
class GeometryNode;
|
||||
|
||||
class CanvasDrawingArea : public Widget
|
||||
{
|
||||
public:
|
||||
~CanvasDrawingArea();
|
||||
void setActiveDrawingCommand(CanvasDrawCommand command)
|
||||
{
|
||||
mActiveDrawingCommand = command;
|
||||
}
|
||||
|
||||
private:
|
||||
bool isDirty() const override;
|
||||
void doPaint(const PaintEvent* event) override;
|
||||
|
||||
void onMyMouseEvent(const MouseEvent* event) override;
|
||||
|
||||
void addShapeAt(unsigned x, unsigned y);
|
||||
|
||||
CanvasDrawCommand mActiveDrawingCommand{CanvasDrawCommand::LINE};
|
||||
|
||||
std::vector<std::unique_ptr<GeometryNode> > mSceneNodes;
|
||||
bool mContentDirty{false};
|
||||
};
|
7
apps/notes_tk/canvas/CanvasElements.h
Normal file
7
apps/notes_tk/canvas/CanvasElements.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
enum class CanvasDrawCommand
|
||||
{
|
||||
CIRCLE,
|
||||
LINE
|
||||
};
|
|
@ -4,15 +4,16 @@
|
|||
#include "VerticalSpacer.h"
|
||||
|
||||
#include "CanvasController.h"
|
||||
#include "CanvasDrawingArea.h"
|
||||
#include "CanvasCommandSelectorView.h"
|
||||
|
||||
#include "Theme.h"
|
||||
|
||||
#include "TextNode.h"
|
||||
#include "GeometryNode.h"
|
||||
|
||||
#include "Label.h"
|
||||
#include "Button.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
CanvasView::CanvasView()
|
||||
: mController(CanvasController::Create())
|
||||
{
|
||||
|
@ -36,11 +37,28 @@ void CanvasView::initialize()
|
|||
label->setBackgroundColor(Theme::getBannerBackground());
|
||||
label->setMargin(1);
|
||||
|
||||
auto controls = std::make_unique<CanvasCommandSelectorView>();
|
||||
controls->setBackgroundColor(Theme::getBannerBackground());
|
||||
controls->setMargin(1);
|
||||
|
||||
auto on_draw_command_changed = [this](CanvasDrawCommand command){
|
||||
onDrawCommandChanged(command);
|
||||
};
|
||||
controls->setCommandSelectedCallback(on_draw_command_changed);
|
||||
|
||||
auto drawing_area = std::make_unique<CanvasDrawingArea>();
|
||||
drawing_area->setBackgroundColor(Theme::getBackgroundPrimary());
|
||||
drawing_area->setMargin(1);
|
||||
mDrawingArea = drawing_area.get();
|
||||
|
||||
auto control_draw_vspacer = VerticalSpacer::Create();
|
||||
control_draw_vspacer->addWidgetWithScale(std::move(controls), 1);
|
||||
control_draw_vspacer->addWidgetWithScale(std::move(drawing_area), 10);
|
||||
|
||||
auto hSpacer = HorizontalSpacer::Create();
|
||||
hSpacer->addWidgetWithScale(std::move(label), 1);
|
||||
//hSpacer->addWidgetWithScale(std::move(textBox), 14);
|
||||
|
||||
hSpacer->addWidgetWithScale(std::move(control_draw_vspacer), 10);
|
||||
|
||||
auto cache_button_spacer = initializeCacheButtons();
|
||||
hSpacer->addWidgetWithScale(std::move(cache_button_spacer), 1);
|
||||
|
@ -48,6 +66,11 @@ void CanvasView::initialize()
|
|||
addWidget(std::move(hSpacer));
|
||||
}
|
||||
|
||||
void CanvasView::onDrawCommandChanged(CanvasDrawCommand command)
|
||||
{
|
||||
mDrawingArea->setActiveDrawingCommand(command);
|
||||
}
|
||||
|
||||
std::unique_ptr<Widget> CanvasView::initializeCacheButtons()
|
||||
{
|
||||
auto saveButton = Button::Create();
|
||||
|
@ -66,10 +89,9 @@ std::unique_ptr<Widget> CanvasView::initializeCacheButtons()
|
|||
loadButton->setMargin(2);
|
||||
|
||||
auto buttonSpacer = VerticalSpacer::Create();
|
||||
buttonSpacer->AddWidgetWithScale(std::move(saveButton), 1);
|
||||
buttonSpacer->AddWidgetWithScale(std::move(clearButton), 1);
|
||||
buttonSpacer->AddWidgetWithScale(std::move(loadButton), 1);
|
||||
|
||||
buttonSpacer->addWidgetWithScale(std::move(saveButton), 1);
|
||||
buttonSpacer->addWidgetWithScale(std::move(clearButton), 1);
|
||||
buttonSpacer->addWidgetWithScale(std::move(loadButton), 1);
|
||||
return buttonSpacer;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "Widget.h"
|
||||
#include "CanvasElements.h"
|
||||
|
||||
class CanvasController;
|
||||
class CanvasDrawingArea;
|
||||
|
||||
class CanvasView : public Widget
|
||||
{
|
||||
|
@ -14,13 +16,14 @@ public:
|
|||
static std::unique_ptr<CanvasView> Create();
|
||||
|
||||
private:
|
||||
void onDrawCommandChanged(CanvasDrawCommand command);
|
||||
|
||||
void initialize();
|
||||
|
||||
std::unique_ptr<Widget> initializeCacheButtons();
|
||||
|
||||
//std::unique_ptr<Widget> initializeCacheButtons();
|
||||
|
||||
std::unique_ptr<CanvasController> mController;
|
||||
|
||||
CanvasDrawingArea* mDrawingArea{nullptr};
|
||||
};
|
||||
using CanvasViewPtr = std::unique_ptr<CanvasView>;
|
||||
|
|
|
@ -72,9 +72,9 @@ void TextEditorView::initialize()
|
|||
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);
|
||||
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);
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
class TextEditorView : public Widget
|
||||
{
|
||||
public:
|
||||
|
||||
TextEditorView();
|
||||
|
||||
static std::unique_ptr<TextEditorView> Create();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue