NanoSubWidgets.cpp (4308B)
1 /* 2 * DISTRHO Plugin Framework (DPF) 3 * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any purpose with 6 * or without fee is hereby granted, provided that the above copyright notice and this 7 * permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD 10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN 11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include "tests.hpp" 18 19 #include "../dgl/NanoVG.hpp" 20 21 START_NAMESPACE_DGL 22 23 // -------------------------------------------------------------------------------------------------------------------- 24 25 class NanoRectangle : public NanoSubWidget 26 { 27 public: 28 explicit NanoRectangle(Widget* const parent) 29 : NanoSubWidget(parent), 30 color() {} 31 32 void setColor(const Color c) noexcept 33 { 34 color = c; 35 } 36 37 protected: 38 void onNanoDisplay() override 39 { 40 beginPath(); 41 42 fillColor(color); 43 rect(0, 0, getWidth(), getHeight()); 44 fill(); 45 46 closePath(); 47 } 48 49 private: 50 Color color; 51 }; 52 53 // -------------------------------------------------------------------------------------------------------------------- 54 55 class NanoRectanglesContainer : public NanoTopLevelWidget, 56 public IdleCallback 57 { 58 public: 59 explicit NanoRectanglesContainer(Window& parent) 60 : NanoTopLevelWidget(parent), 61 rect1(this), 62 rect2(this), 63 rect3(this), 64 rectToHide(1) 65 { 66 rect1.setAbsolutePos(100, 100); 67 rect1.setSize(25, 25); 68 rect1.setColor(Color(255, 0, 0)); 69 70 rect2.setAbsolutePos(200, 200); 71 rect2.setSize(25, 25); 72 rect2.setColor(Color(0, 255, 0)); 73 74 rect3.setAbsolutePos(300, 300); 75 rect3.setSize(25, 25); 76 rect3.setColor(Color(0, 0, 255)); 77 78 idleCallback(); 79 addIdleCallback(this, 500); 80 } 81 82 protected: 83 void onNanoDisplay() override 84 { 85 beginPath(); 86 87 fillColor(Color(0.5f, 0.5f, 0.5f)); 88 rect(0, 0, getWidth(), getHeight()); 89 fill(); 90 91 closePath(); 92 } 93 94 void idleCallback() override 95 { 96 switch (rectToHide) 97 { 98 case 1: 99 rect1.hide(); 100 rect2.show(); 101 rect3.show(); 102 rectToHide = 2; 103 break; 104 case 2: 105 rect1.show(); 106 rect2.hide(); 107 rect3.show(); 108 rectToHide = 3; 109 break; 110 case 3: 111 rect1.show(); 112 rect2.show(); 113 rect3.hide(); 114 rectToHide = 1; 115 break; 116 } 117 } 118 119 private: 120 NanoRectangle rect1, rect2, rect3; 121 int rectToHide; 122 }; 123 124 // -------------------------------------------------------------------------------------------------------------------- 125 126 class NanoExampleWindow : public Window 127 { 128 public: 129 explicit NanoExampleWindow(Application& app) 130 : Window(app) 131 { 132 { 133 const ScopedGraphicsContext sgc(*this); 134 container = new NanoRectanglesContainer(*this); 135 } 136 137 const uint targetWidth = 400; 138 const uint targetHeight = 400; 139 const double scaleFactor = getScaleFactor(); 140 141 setGeometryConstraints(targetWidth, targetHeight, true); 142 setResizable(true); 143 setSize(targetWidth * scaleFactor, targetHeight * scaleFactor); 144 setTitle("NanoVG SubWidgets test"); 145 } 146 147 private: 148 ScopedPointer<NanoRectanglesContainer> container; 149 }; 150 151 // -------------------------------------------------------------------------------------------------------------------- 152 153 END_NAMESPACE_DGL 154 155 int main() 156 { 157 USE_NAMESPACE_DGL; 158 159 Application app; 160 NanoExampleWindow win(app); 161 win.show(); 162 app.exec(); 163 164 return 0; 165 } 166 167 // --------------------------------------------------------------------------------------------------------------------