ExampleColorWidget.hpp (4610B)
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 #ifndef EXAMPLE_COLOR_WIDGET_HPP_INCLUDED 18 #define EXAMPLE_COLOR_WIDGET_HPP_INCLUDED 19 20 // ------------------------------------------------------ 21 // DGL Stuff 22 23 #include "../../dgl/Color.hpp" 24 #include "../../dgl/StandaloneWindow.hpp" 25 #include "../../dgl/SubWidget.hpp" 26 27 START_NAMESPACE_DGL 28 29 // ------------------------------------------------------ 30 // our widget 31 32 template <class BaseWidget> 33 class ExampleColorWidget : public BaseWidget, 34 public IdleCallback 35 { 36 char cur; 37 bool reverse; 38 int r, g, b; 39 40 Rectangle<uint> bgFull, bgSmall; 41 42 public: 43 static constexpr const char* kExampleWidgetName = "Color"; 44 45 // SubWidget 46 explicit ExampleColorWidget(Widget* const parent); 47 48 // TopLevelWidget 49 explicit ExampleColorWidget(Window& windowToMapTo); 50 51 // StandaloneWindow 52 explicit ExampleColorWidget(Application& app); 53 54 protected: 55 void idleCallback() noexcept override 56 { 57 switch (cur) 58 { 59 case 'r': 60 if (reverse) 61 { 62 if (--r == 0) 63 cur = 'g'; 64 } 65 else 66 { 67 if (++r == 100) 68 cur = 'g'; 69 } 70 break; 71 72 case 'g': 73 if (reverse) 74 { 75 if (--g == 0) 76 cur = 'b'; 77 } 78 else 79 { 80 if (++g == 100) 81 cur = 'b'; 82 } 83 break; 84 85 case 'b': 86 if (reverse) 87 { 88 if (--b == 0) 89 { 90 cur = 'r'; 91 reverse = false; 92 } 93 } 94 else 95 { 96 if (++b == 100) 97 { 98 cur = 'r'; 99 reverse = true; 100 } 101 } 102 break; 103 } 104 105 BaseWidget::repaint(); 106 } 107 108 void onDisplay() override 109 { 110 const GraphicsContext& context(BaseWidget::getGraphicsContext()); 111 112 // paint bg color (in full size) 113 Color(r, g, b).setFor(context); 114 bgFull.draw(context); 115 116 // paint inverted color (in 2/3 size) 117 Color(100-r, 100-g, 100-b).setFor(context); 118 bgSmall.draw(context); 119 } 120 121 void onResize(const Widget::ResizeEvent& ev) override 122 { 123 const uint width = ev.size.getWidth(); 124 const uint height = ev.size.getHeight(); 125 126 // full bg 127 bgFull = Rectangle<uint>(0, 0, width, height); 128 129 // small bg, centered 2/3 size 130 bgSmall = Rectangle<uint>(width/6, height/6, width*2/3, height*2/3); 131 } 132 }; 133 134 // SubWidget 135 template<> inline 136 ExampleColorWidget<SubWidget>::ExampleColorWidget(Widget* const parent) 137 : SubWidget(parent), 138 cur('r'), 139 reverse(false), 140 r(0), g(0), b(0) 141 { 142 setSize(300, 300); 143 parent->getApp().addIdleCallback(this); 144 } 145 146 // TopLevelWidget 147 template<> inline 148 ExampleColorWidget<TopLevelWidget>::ExampleColorWidget(Window& windowToMapTo) 149 : TopLevelWidget(windowToMapTo), 150 cur('r'), 151 reverse(false), 152 r(0), g(0), b(0) 153 { 154 setSize(300, 300); 155 addIdleCallback(this); 156 } 157 158 // StandaloneWindow 159 template<> inline 160 ExampleColorWidget<StandaloneWindow>::ExampleColorWidget(Application& app) 161 : StandaloneWindow(app), 162 cur('r'), 163 reverse(false), 164 r(0), g(0), b(0) 165 { 166 setSize(300, 300); 167 addIdleCallback(this); 168 done(); 169 } 170 171 typedef ExampleColorWidget<SubWidget> ExampleColorSubWidget; 172 typedef ExampleColorWidget<TopLevelWidget> ExampleColorTopLevelWidget; 173 typedef ExampleColorWidget<StandaloneWindow> ExampleColorStandaloneWindow; 174 175 // ------------------------------------------------------ 176 177 END_NAMESPACE_DGL 178 179 #endif // EXAMPLE_COLOR_WIDGET_HPP_INCLUDED