From 36d848b1930fcfbf00fb2ea2e8fbd1dd39be7b90 Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 13 May 2014 15:36:52 +0100 Subject: [PATCH] Add rectangles example --- dpf | 2 +- examples/Makefile | 8 +- examples/color.cpp | 6 +- examples/demo.cpp | 7 +- examples/rectangles.cpp | 168 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 9 deletions(-) create mode 100644 examples/rectangles.cpp diff --git a/dpf b/dpf index d17a870..cb0d997 160000 --- a/dpf +++ b/dpf @@ -1 +1 @@ -Subproject commit d17a8706cc9d0c4540eb6bb09dd3357f9e3a8437 +Subproject commit cb0d99729f73be3273115ca058f2d6effa3c94c1 diff --git a/examples/Makefile b/examples/Makefile index 1ff22bb..64bc326 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -14,9 +14,10 @@ LINK_FLAGS += -L../dpf -ldgl $(DGL_LIBS) # -------------------------------------------------------------- ifeq ($(WIN32),true) -TARGETS = app.exe color.exe demo.exe images.exe +TARGETS = app.exe color.exe demo.exe images.exe rectangles.exe +LINK_FLAGS += -mwindows else -TARGETS = app color demo images +TARGETS = app color demo images rectangles endif # -------------------------------------------------------------- @@ -54,6 +55,9 @@ demo: demo.cpp ../dpf/dgl/* images: images.cpp images_src/* ../dpf/dgl/* $(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@ +rectangles: rectangles.cpp ../dpf/dgl/* + $(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@ + # -------------------------------------------------------------- cairo: cairo.cpp ../dgl/* diff --git a/examples/color.cpp b/examples/color.cpp index 0aaee0e..aaa6843 100644 --- a/examples/color.cpp +++ b/examples/color.cpp @@ -110,10 +110,10 @@ private: void onReshape(int width, int height) override { // full bg - bgFull = Rectangle(0, 0, width, height); + bgFull = DGL::Rectangle(0, 0, width, height); // small bg, centered 2/3 size - bgSmall = Rectangle(width/6, height/6, width*2/3, height*2/3); + bgSmall = DGL::Rectangle(width/6, height/6, width*2/3, height*2/3); // make widget same size as window setSize(width, height); @@ -126,7 +126,7 @@ private: bool reverse; int r, g, b; - Rectangle bgFull, bgSmall; + DGL::Rectangle bgFull, bgSmall; }; // ------------------------------------------------------ diff --git a/examples/demo.cpp b/examples/demo.cpp index 3126ddb..95e27df 100644 --- a/examples/demo.cpp +++ b/examples/demo.cpp @@ -33,14 +33,15 @@ class DemoWindow : public Window { public: DemoWindow(App& app) - : Window(app) + : Window(app)//, + //w1(*this) { setSize(300, 300); setTitle("DGL Demo"); } -private: - // here +//private: + //Widget_Geometry w1; }; // ------------------------------------------------------ diff --git a/examples/rectangles.cpp b/examples/rectangles.cpp new file mode 100644 index 0000000..1c7c8b2 --- /dev/null +++ b/examples/rectangles.cpp @@ -0,0 +1,168 @@ +/* + * DISTRHO Plugin Framework (DPF) + * Copyright (C) 2012-2014 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. + */ + +// ------------------------------------------------------ +// DGL Stuff + +#include "App.hpp" +#include "Window.hpp" +#include "Widget.hpp" + +// ------------------------------------------------------ +// use namespace + +using namespace DGL; + +// ------------------------------------------------------ +// Geometry + +class RectanglesWidget : public Widget +{ +public: + RectanglesWidget(Window& parent) + : Widget(parent) + { + for (int i=0; i<9; ++i) + fClicked[i] = false; + } + +protected: + void onDisplay() override + { + const int width = getWidth(); + const int height = getHeight(); + + DGL::Rectangle r; + + r.setWidth(width/3 - 6); + r.setHeight(height/3 - 6); + + // draw a 3x3 grid + for (int i=0; i<3; ++i) + { + r.setX(3 + i*width/3); + + // 1st + r.setY(3); + + if (fClicked[0+i]) + glColor3f(0.8f, 0.5f, 0.3f); + else + glColor3f(0.3f, 0.5f, 0.8f); + + r.draw(); + + // 2nd + r.setY(3 + height/3); + + if (fClicked[3+i]) + glColor3f(0.8f, 0.5f, 0.3f); + else + glColor3f(0.3f, 0.5f, 0.8f); + + r.draw(); + + // 3rd + r.setY(3 + height*2/3); + + if (fClicked[6+i]) + glColor3f(0.8f, 0.5f, 0.3f); + else + glColor3f(0.3f, 0.5f, 0.8f); + + r.draw(); + } + } + + void onReshape(int width, int height) override + { + // make this widget same size as window + setSize(width, height); + Widget::onReshape(width, height); + } + + bool onMouse(int button, bool press, int x, int y) override + { + if (button != 1 || ! press) + return false; + + const Point pos(x, y); + + const int width = getWidth(); + const int height = getHeight(); + + DGL::Rectangle r; + + r.setWidth(width/3 - 6); + r.setHeight(height/3 - 6); + + // draw a 3x3 grid + for (int i=0; i<3; ++i) + { + r.setX(3 + i*width/3); + r.setY(3); + + if (r.contains(pos)) + { + fClicked[0+i] = !fClicked[0+i]; + repaint(); + break; + } + + r.setY(3 + height/3); + + if (r.contains(pos)) + { + fClicked[3+i] = !fClicked[3+i]; + repaint(); + break; + } + + r.setY(3 + height*2/3); + + if (r.contains(pos)) + { + fClicked[6+i] = !fClicked[6+i]; + repaint(); + break; + } + } + + return true; + } + +private: + bool fClicked[9]; +}; + +// ------------------------------------------------------ +// main entry point + +int main() +{ + App app; + Window win(app); + RectanglesWidget rects(win); + + win.setSize(300, 300); + win.setTitle("Rectangles"); + win.show(); + app.exec(); + + return 0; +} + +// ------------------------------------------------------