diff --git a/dgl/Cairo.hpp b/dgl/Cairo.hpp index 9a896ec7..d0f8014e 100644 --- a/dgl/Cairo.hpp +++ b/dgl/Cairo.hpp @@ -17,6 +17,8 @@ #ifndef DGL_CAIRO_HPP_INCLUDED #define DGL_CAIRO_HPP_INCLUDED +#include "ImageBase.hpp" +#include "ImageBaseWidgets.hpp" #include "SubWidget.hpp" #include "TopLevelWidget.hpp" @@ -34,6 +36,54 @@ struct CairoGraphicsContext : GraphicsContext cairo_t* handle; }; +// -------------------------------------------------------------------------------------------------------------------- + +/** + Cairo Image class. + + TODO ... + */ +class CairoImage : public ImageBase +{ +public: + /** + Constructor for a null Image. + */ + CairoImage(); + + /** + Constructor using raw image data. + @note @a rawData must remain valid for the lifetime of this Image. + */ + CairoImage(const char* const rawData, + const uint width, + const uint height); + + /** + Constructor using raw image data. + @note @a rawData must remain valid for the lifetime of this Image. + */ + CairoImage(const char* const rawData, + const Size& size); + + /** + Constructor using another image data. + */ + CairoImage(const CairoImage& image); + + /** + Destructor. + */ + ~CairoImage() override; + + /** + Draw this image at position @a pos using the graphics context @a context. + */ + void drawAt(const GraphicsContext& context, const Point& pos) override; +}; + +// -------------------------------------------------------------------------------------------------------------------- + /** Cairo SubWidget, handy class that takes graphics context during onDisplay and passes it in a new function. */ @@ -78,6 +128,10 @@ protected: // -------------------------------------------------------------------------------------------------------------------- +typedef ImageBaseAboutWindow CairoImageAboutWindow; + +// -------------------------------------------------------------------------------------------------------------------- + END_NAMESPACE_DGL #endif diff --git a/dgl/ImageBaseWidgets.hpp b/dgl/ImageBaseWidgets.hpp new file mode 100644 index 00000000..3fef7e63 --- /dev/null +++ b/dgl/ImageBaseWidgets.hpp @@ -0,0 +1,53 @@ +/* + * DISTRHO Plugin Framework (DPF) + * Copyright (C) 2012-2021 Filipe Coelho + * + * Permission to use, copy, modify, and/or distribute this software for any purpose with + * or without fee is hereby granted, provided that the above copyright notice and this + * permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD + * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef DGL_IMAGE_BASE_WIDGETS_HPP_INCLUDED +#define DGL_IMAGE_BASE_WIDGETS_HPP_INCLUDED + +#include "StandaloneWindow.hpp" + +START_NAMESPACE_DGL + +// -------------------------------------------------------------------------------------------------------------------- + +template +class ImageBaseAboutWindow : public StandaloneWindow +{ +public: + explicit ImageBaseAboutWindow(Window& parentWindow, const ImageType& image = ImageType()); + explicit ImageBaseAboutWindow(TopLevelWidget* parentTopLevelWidget, const ImageType& image = ImageType()); + + void setImage(const ImageType& image); + +protected: + void onDisplay() override; + bool onKeyboard(const KeyboardEvent&) override; + bool onMouse(const MouseEvent&) override; + + // FIXME needed? + void onReshape(uint width, uint height) override; + +private: + ImageType img; + + DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageBaseAboutWindow) +}; + +// -------------------------------------------------------------------------------------------------------------------- + +END_NAMESPACE_DGL + +#endif // DGL_IMAGE_BASE_WIDGETS_HPP_INCLUDED diff --git a/dgl/ImageWidgets.hpp b/dgl/ImageWidgets.hpp index ec465320..245daea9 100644 --- a/dgl/ImageWidgets.hpp +++ b/dgl/ImageWidgets.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2016 Filipe Coelho + * Copyright (C) 2012-2021 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -18,38 +18,13 @@ #define DGL_IMAGE_WIDGETS_HPP_INCLUDED #include "Image.hpp" -#include "StandaloneWindow.hpp" +#include "ImageBaseWidgets.hpp" #include "SubWidget.hpp" START_NAMESPACE_DGL // ----------------------------------------------------------------------- -class ImageAboutWindow : public StandaloneWindow -{ -public: - explicit ImageAboutWindow(Window& parentWindow, const Image& image = Image()); - explicit ImageAboutWindow(TopLevelWidget* parentTopLevelWidget, const Image& image = Image()); - - void setImage(const Image& image); - - // TODO - void exec() {} - -protected: - void onDisplay() override; - bool onKeyboard(const KeyboardEvent&) override; - bool onMouse(const MouseEvent&) override; - void onReshape(uint width, uint height) override; - -private: - Image fImgBackground; - - DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageAboutWindow) -}; - -// ----------------------------------------------------------------------- - class ImageButton : public SubWidget { public: diff --git a/dgl/Makefile b/dgl/Makefile index b8a3db4f..2e228b68 100644 --- a/dgl/Makefile +++ b/dgl/Makefile @@ -29,6 +29,7 @@ OBJS_common = \ ../build/dgl/Color.cpp.o \ ../build/dgl/Geometry.cpp.o \ ../build/dgl/ImageBase.cpp.o \ + ../build/dgl/ImageBaseWidgets.cpp.o \ ../build/dgl/Resources.cpp.o \ ../build/dgl/SubWidget.cpp.o \ ../build/dgl/SubWidgetPrivateData.cpp.o \ diff --git a/dgl/OpenGL.hpp b/dgl/OpenGL.hpp index b0f37252..55905bec 100644 --- a/dgl/OpenGL.hpp +++ b/dgl/OpenGL.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2019 Filipe Coelho + * Copyright (C) 2012-2021 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -18,6 +18,7 @@ #define DGL_OPENGL_HPP_INCLUDED #include "ImageBase.hpp" +#include "ImageBaseWidgets.hpp" // ----------------------------------------------------------------------- // Fix OpenGL includes for Windows, based on glfw code (part 1) @@ -241,6 +242,13 @@ private: // ----------------------------------------------------------------------- +typedef ImageBaseAboutWindow OpenGLImageAboutWindow; + +// TODO deprecated +typedef OpenGLImageAboutWindow ImageAboutWindow; + +// ----------------------------------------------------------------------- + END_NAMESPACE_DGL #endif diff --git a/dgl/StandaloneWindow.hpp b/dgl/StandaloneWindow.hpp index 85bfd4b7..5db33926 100644 --- a/dgl/StandaloneWindow.hpp +++ b/dgl/StandaloneWindow.hpp @@ -29,12 +29,19 @@ class StandaloneWindow : public Window, { public: /** - Constructor. + Constructor without parent. */ StandaloneWindow(Application& app) : Window(app), TopLevelWidget((Window&)*this) {} + /** + Constructor with parent window, typically used to run as modal. + */ + StandaloneWindow(Application& app, Window& parent) + : Window(app, parent), + TopLevelWidget((Window&)*this) {} + /** Overloaded functions to ensure they apply to the Window class. */ diff --git a/dgl/src/Cairo.cpp b/dgl/src/Cairo.cpp index 1f423c0d..79eca970 100644 --- a/dgl/src/Cairo.cpp +++ b/dgl/src/Cairo.cpp @@ -63,6 +63,37 @@ void Rectangle::_draw(const bool outline) notImplemented("Rectangle::draw"); } +// ----------------------------------------------------------------------- +// CairoImage + +CairoImage::CairoImage() + : ImageBase() {} + +CairoImage::CairoImage(const char* const rawData, const uint width, const uint height) + : ImageBase(rawData, width, height) {} + +CairoImage::CairoImage(const char* const rawData, const Size& size) + : ImageBase(rawData, size) {} + +CairoImage::CairoImage(const CairoImage& image) + : ImageBase(image.rawData, image.size) {} + +CairoImage::~CairoImage() +{ +} + +void CairoImage::drawAt(const GraphicsContext&, const Point&) +{ +} + +// ----------------------------------------------------------------------- + +template <> +void ImageBaseAboutWindow::onDisplay() +{ + img.draw(getGraphicsContext()); +} + // ----------------------------------------------------------------------- void SubWidget::PrivateData::display(const uint width, const uint height, const double autoScaleFactor) @@ -97,6 +128,39 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept return context; } +// ----------------------------------------------------------------------- +// Possible template data types + +template class Line; +template class Line; +template class Line; +template class Line; +template class Line; +template class Line; + +template class Circle; +template class Circle; +template class Circle; +template class Circle; +template class Circle; +template class Circle; + +template class Triangle; +template class Triangle; +template class Triangle; +template class Triangle; +template class Triangle; +template class Triangle; + +template class Rectangle; +template class Rectangle; +template class Rectangle; +template class Rectangle; +template class Rectangle; +template class Rectangle; + +template class ImageBaseAboutWindow; + // ----------------------------------------------------------------------- END_NAMESPACE_DGL diff --git a/dgl/src/ImageBaseWidgets.cpp b/dgl/src/ImageBaseWidgets.cpp new file mode 100644 index 00000000..e01e2b10 --- /dev/null +++ b/dgl/src/ImageBaseWidgets.cpp @@ -0,0 +1,91 @@ +/* + * DISTRHO Plugin Framework (DPF) + * Copyright (C) 2012-2021 Filipe Coelho + * + * Permission to use, copy, modify, and/or distribute this software for any purpose with + * or without fee is hereby granted, provided that the above copyright notice and this + * permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD + * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "../ImageBaseWidgets.hpp" + +START_NAMESPACE_DGL + +// -------------------------------------------------------------------------------------------------------------------- + +template +ImageBaseAboutWindow::ImageBaseAboutWindow(Window& parentWindow, const ImageType& image) + : StandaloneWindow(parentWindow.getApp(), parentWindow), + img(image) +{ + setResizable(false); + setTitle("About"); + + if (image.isValid()) + setSize(image.getSize()); +} + +template +ImageBaseAboutWindow::ImageBaseAboutWindow(TopLevelWidget* const parentTopLevelWidget, const ImageType& image) + : StandaloneWindow(parentTopLevelWidget->getApp(), parentTopLevelWidget->getWindow()), + img(image) +{ + setResizable(false); + setTitle("About"); + + if (image.isValid()) + setSize(image.getSize()); +} + +template +void ImageBaseAboutWindow::setImage(const ImageType& image) +{ + if (img == image) + return; + + img = image; + setSize(image.getSize()); +} + +template +bool ImageBaseAboutWindow::onKeyboard(const KeyboardEvent& ev) +{ + if (ev.press && ev.key == kCharEscape) + { + close(); + return true; + } + + return false; +} + +template +bool ImageBaseAboutWindow::onMouse(const MouseEvent& ev) +{ + if (ev.press) + { + close(); + return true; + } + + return false; +} + +template +void ImageBaseAboutWindow::onReshape(uint width, uint height) +{ + // FIXME needed? + TopLevelWidget::setSize(width, height); + StandaloneWindow::onReshape(width, height); +} + +// -------------------------------------------------------------------------------------------------------------------- + +END_NAMESPACE_DGL diff --git a/dgl/src/ImageWidgets.cpp b/dgl/src/ImageWidgets.cpp index 80221b23..a22d8b6e 100644 --- a/dgl/src/ImageWidgets.cpp +++ b/dgl/src/ImageWidgets.cpp @@ -15,6 +15,8 @@ */ #include "../Image.hpp" +#include "../ImageBaseWidgets.hpp" + #include "Common.hpp" #include "WidgetPrivateData.hpp" @@ -25,74 +27,6 @@ START_NAMESPACE_DGL // ----------------------------------------------------------------------- -ImageAboutWindow::ImageAboutWindow(Window& parentWindow, const Image& image) - : StandaloneWindow(parentWindow.getApp()), - fImgBackground(image) -{ - // TODO set transient - Window::setResizable(false); - Window::setTitle("About"); - - if (image.isValid()) - Window::setSize(image.getSize()); -} - -ImageAboutWindow::ImageAboutWindow(TopLevelWidget* const parentTopLevelWidget, const Image& image) - : StandaloneWindow(parentTopLevelWidget->getApp()), - fImgBackground(image) -{ - // TODO set transient - Window::setResizable(false); - Window::setTitle("About"); - - if (image.isValid()) - Window::setSize(image.getSize()); -} - -void ImageAboutWindow::setImage(const Image& image) -{ - if (fImgBackground == image) - return; - - fImgBackground = image; - Window::setSize(image.getSize()); -} - -void ImageAboutWindow::onDisplay() -{ - fImgBackground.draw(); -} - -bool ImageAboutWindow::onKeyboard(const KeyboardEvent& ev) -{ - if (ev.press && ev.key == kCharEscape) - { - Window::close(); - return true; - } - - return false; -} - -bool ImageAboutWindow::onMouse(const MouseEvent& ev) -{ - if (ev.press) - { - Window::close(); - return true; - } - - return false; -} - -void ImageAboutWindow::onReshape(uint width, uint height) -{ - Widget::setSize(width, height); - Window::onReshape(width, height); -} - -// ----------------------------------------------------------------------- - struct ImageButton::PrivateData { ButtonImpl impl; Image imageNormal; diff --git a/dgl/src/OpenGL.cpp b/dgl/src/OpenGL.cpp index 13021068..d9fecc8a 100644 --- a/dgl/src/OpenGL.cpp +++ b/dgl/src/OpenGL.cpp @@ -15,6 +15,8 @@ */ #include "../OpenGL.hpp" +#include "../ImageWidgets.hpp" + #include "SubWidgetPrivateData.hpp" #include "TopLevelWidgetPrivateData.hpp" #include "WidgetPrivateData.hpp" @@ -110,39 +112,6 @@ void Rectangle::_draw(const bool outline) glEnd(); } -// ----------------------------------------------------------------------- -// Possible template data types - -#ifndef DPF_TEST_DEMO -template class Line; -template class Line; -template class Line; -template class Line; -template class Line; -template class Line; - -template class Circle; -template class Circle; -template class Circle; -template class Circle; -template class Circle; -template class Circle; - -template class Triangle; -template class Triangle; -template class Triangle; -template class Triangle; -template class Triangle; -template class Triangle; - -template class Rectangle; -template class Rectangle; -template class Rectangle; -template class Rectangle; -template class Rectangle; -template class Rectangle; -#endif - // ----------------------------------------------------------------------- OpenGLImage::OpenGLImage() @@ -152,13 +121,6 @@ OpenGLImage::OpenGLImage() fTextureId(0), setupCalled(false) {} -OpenGLImage::OpenGLImage(const OpenGLImage& image) - : ImageBase(image), - fFormat(image.fFormat), - fType(image.fType), - fTextureId(0), - setupCalled(false) {} - OpenGLImage::OpenGLImage(const char* const rawData, const uint width, const uint height, const GLenum format, const GLenum type) : ImageBase(rawData, width, height), fFormat(format), @@ -173,6 +135,13 @@ OpenGLImage::OpenGLImage(const char* const rawData, const Size& size, cons fTextureId(0), setupCalled(false) {} +OpenGLImage::OpenGLImage(const OpenGLImage& image) + : ImageBase(image), + fFormat(image.fFormat), + fType(image.fType), + fTextureId(0), + setupCalled(false) {} + OpenGLImage::~OpenGLImage() { if (setupCalled) { @@ -324,72 +293,13 @@ void OpenGLImage::drawAt(const Point& pos) // ----------------------------------------------------------------------- -#if 0 -void Widget::PrivateData::display(const uint width, - const uint height, - const double autoScaleFactor, - const bool renderingSubWidget) +template <> +void ImageBaseAboutWindow::onDisplay() { - printf("Widget::PrivateData::display INIT\n"); - - if (/*(skipDisplay && ! renderingSubWidget) ||*/ size.isInvalid() || ! visible) - return; - - bool needsDisableScissor = false; - - // reset color - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - -#if 0 - if (/*needsFullViewport ||*/ (absolutePos.isZero() && size == Size(width, height))) -#endif - { - // full viewport size - glViewport(0, - -(height * autoScaleFactor - height), - width * autoScaleFactor, - height * autoScaleFactor); - } -#if 0 - else if (needsScaling) - { - // limit viewport to widget bounds - glViewport(absolutePos.getX(), - height - self->getHeight() - absolutePos.getY(), - self->getWidth(), - self->getHeight()); - } - else - { - // only set viewport pos - glViewport(absolutePos.getX() * autoScaleFactor, - -std::round((height * autoScaleFactor - height) + (absolutePos.getY() * autoScaleFactor)), - std::round(width * autoScaleFactor), - std::round(height * autoScaleFactor)); - - // then cut the outer bounds - glScissor(absolutePos.getX() * autoScaleFactor, - height - std::round((self->getHeight() + absolutePos.getY()) * autoScaleFactor), - std::round(self->getWidth() * autoScaleFactor), - std::round(self->getHeight() * autoScaleFactor)); - - glEnable(GL_SCISSOR_TEST); - needsDisableScissor = true; - } -#endif - - // display widget - self->onDisplay(); - - if (needsDisableScissor) - { - glDisable(GL_SCISSOR_TEST); - needsDisableScissor = false; - } - - displaySubWidgets(width, height, autoScaleFactor); + img.draw(); } -#endif + +// ----------------------------------------------------------------------- void SubWidget::PrivateData::display(const uint width, const uint height, const double autoScaleFactor) { @@ -453,9 +363,9 @@ void TopLevelWidget::PrivateData::display() // full viewport size if (window.pData->autoScaling) - glViewport(0, -height, width, height); - else glViewport(0, -(height * autoScaleFactor - height), width * autoScaleFactor, height * autoScaleFactor); + else + glViewport(0, -height, width, height); // main widget drawing self->onDisplay(); @@ -472,5 +382,52 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept } // ----------------------------------------------------------------------- +// Possible template data types + +#ifndef DPF_TEST_DEMO +// // FIXME +// template class Line; +// template class Line; +// template class Line; +// template class Line; +// template class Line; +// template class Line; +// +// template class Circle; +// template class Circle; +// template class Circle; +// template class Circle; +// template class Circle; +// template class Circle; +// +// template class Triangle; +// template class Triangle; +// template class Triangle; +// template class Triangle; +// template class Triangle; +// template class Triangle; +// +template class Rectangle; +template class Rectangle; +template class Rectangle; +template class Rectangle; +template class Rectangle; +template class Rectangle; +#endif + +// ----------------------------------------------------------------------- + +END_NAMESPACE_DGL + +// ----------------------------------------------------------------------- +// templated classes + +#include "ImageBaseWidgets.cpp" + +START_NAMESPACE_DGL + +template class ImageBaseAboutWindow; END_NAMESPACE_DGL + +// -----------------------------------------------------------------------