added Canvas, Brush, Stroke and CanvasNode classes

This commit is contained in:
2017-03-26 00:26:46 +00:00
parent a1e3fd4ecf
commit 744cd8bfb5
8 changed files with 256 additions and 9 deletions

View File

@@ -6,6 +6,7 @@
#include "asset.h"
#include "rtt.h"
#include "bezier.h"
#include "canvas.h"
#include <tinyxml2.h>
#include <yoga/Yoga.h>
@@ -72,7 +73,8 @@ enum class kWidget : uint16_t
PanelColor = const_hash("panel-color"),
PanelStroke = const_hash("panel-stroke"),
ColorQuad = const_hash("color-quad"),
Canvas2D = const_hash("canvas2D"),
StrokePreview = const_hash("stroke-preview"),
Canvas = const_hash("canvas"),
};
enum class kShapeType : uint16_t
@@ -733,15 +735,15 @@ public:
key_capture();
break;
case kEventType::KeyDown:
switch (ke->m_key)
{
// switch (ke->m_key)
// {
// case VK_BACK:
// m_string.erase(m_string.end() - 1);
// m_text->set_text(m_string.c_str());
// break;
default:
break;
}
// default:
// break;
// }
break;
case kEventType::KeyChar:
if (ke->m_key >= 32 && ke->m_key < 32+96)
@@ -1852,4 +1854,83 @@ public:
}
};
class NodeCanvas : public Node
{
bool m_dragging = false;
public:
std::unique_ptr<ui::Canvas> m_canvas;
virtual Node* clone_instantiate() const override { return new NodeCanvas(); }
virtual void init() override
{
m_mouse_ignore = false;
m_canvas = std::make_unique<ui::Canvas>();
m_canvas->create(512, 512);
m_canvas->clear();
}
virtual void draw() override
{
using namespace ui;
//glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle) * 10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
//glm::mat4 proj = glm::perspective<float>(glm::radians(45.f), m_clip.z / m_clip.w, .1f, 100);
GLint vp[4];
GLfloat cc[4];
glGetIntegerv(GL_VIEWPORT, vp);
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
auto box = m_clip * root()->m_zoom;
glm::ivec4 c = (glm::ivec4)glm::vec4(box.x, (int)(vp[3] - box.y - box.w), box.z, box.w);
glViewport(c.x, c.y, c.z, c.w);
glm::vec2 sz = { m_canvas->m_width, m_canvas->m_height };
auto mvp = glm::ortho(0.f, box.z, box.w, 0.f, -1.f, 1.f) *
glm::translate(glm::vec3((m_size - sz) * 0.5f, 0)) *
glm::scale(glm::vec3(sz, 1)) *
glm::translate(glm::vec3(.5f, .5f, 0.f)); // pivot
m_canvas->m_fb.bindTexture();
m_canvas->m_sampler.bind(0);
ui::ShaderManager::use(kShader::Texture);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
NodeBorder::m_plane.draw_fill();
m_canvas->m_sampler.unbind();
m_canvas->m_fb.unbindTexture();
glViewport(vp[0], vp[1], vp[2], vp[3]);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
}
virtual void handle_resize(glm::vec2 old_size, glm::vec2 new_size) override
{
}
virtual kEventResult handle_event(Event* e) override
{
MouseEvent* me = static_cast<MouseEvent*>(e);
Node::handle_event(e);
switch (e->m_type)
{
case kEventType::MouseDownL:
{
auto b = std::make_shared<ui::Brush>();
b->m_texture_id = const_hash("data/Icons/Round-Brush.png");
m_canvas->stroke_start(me->m_pos, 1.f, b);
m_dragging = true;
break;
}
case kEventType::MouseUpL:
m_canvas->stroke_end();
m_dragging = false;
break;
case kEventType::MouseMove:
if (m_dragging)
m_canvas->stroke_update(me->m_pos, 1.f);
break;
default:
break;
}
return kEventResult::Consumed;
}
};