gen-rack

Create VCV Rack modules from gen~ exports
Log | Files | Refs | README | LICENSE

widgets.hpp (2102B)


      1 #include "rack.hpp"
      2 
      3 using namespace rack;
      4 
      5 extern Plugin *pluginInstance;
      6 
      7 
      8 namespace genrack {
      9 
     10 struct Panel : Widget {
     11     NVGcolor color = nvgRGB(255, 255, 255);
     12 	Panel(int r = 255, int g = 255, int b = 255) {
     13         color = nvgRGB(r, g, b);
     14 	}
     15 
     16 	void step() override {
     17 		Widget::step();
     18 	}
     19 
     20 	void draw(const DrawArgs& args) override {
     21 		nvgBeginPath(args.vg);
     22 		nvgRect(args.vg, 0.0, 0.0, box.size.x, box.size.y);
     23 		nvgFillColor(args.vg, color);
     24 		nvgFill(args.vg);
     25 		Widget::draw(args);
     26 	}
     27 };
     28 
     29 struct Title : TransparentWidget {
     30 	std::shared_ptr<Font> font;
     31 	float _x;
     32 	float _y;
     33 	float _w;
     34 	const char* _text;
     35 	NVGcolor _color;
     36 	int _fs;
     37 
     38 	Title(float x, float y, float w, const char* text, NVGcolor color = nvgRGB(230, 230, 230), int fs = 24) {
     39 		_x = x;
     40 		_y = y;
     41 		_w = w;
     42 		_text = text;
     43 		_color = color;
     44 		_fs = fs;
     45 		font = APP->window->loadFont(asset::plugin(pluginInstance, "res/Lato/Lato-Black.ttf"));
     46 	}
     47 
     48 	void draw (const DrawArgs &args) override {
     49 		nvgBeginPath(args.vg);
     50 		nvgFontFaceId(args.vg, font->handle);
     51 		nvgFontSize(args.vg, _fs);
     52 		nvgTextAlign(args.vg, NVG_ALIGN_TOP | NVG_ALIGN_CENTER);
     53 		nvgFillColor(args.vg, _color);
     54 		nvgText(args.vg, _x, _y, _text, NULL);
     55 
     56 		float bounds[4];
     57 		nvgTextBounds(args.vg, _x + _w/2, _y, _text, NULL, bounds);
     58 	}
     59 };
     60 
     61 struct TextLabel : TransparentWidget {
     62 	std::shared_ptr<Font> font;
     63 	float _x;
     64 	float _y;
     65 	float _w;
     66 	const char* _text;
     67 	NVGcolor _color;
     68 	int _fs;
     69 
     70 	TextLabel(float x, float y, float w, const char* text, NVGcolor color = nvgRGB(230, 230, 230), int fs = 12) {
     71 		_x = x;
     72 		_y = y;
     73 		_w = w;
     74 		_text = text;
     75 		_color = color;
     76 		_fs = fs;
     77 		font = APP->window->loadFont(asset::plugin(pluginInstance, "res/Lato/Lato-Regular.ttf"));
     78 	}
     79 
     80 	void draw (const DrawArgs &args) override {
     81 		nvgBeginPath(args.vg);
     82 		nvgFontFaceId(args.vg, font->handle);
     83 		nvgFontSize(args.vg, _fs);
     84 		nvgTextAlign(args.vg, NVG_ALIGN_TOP | NVG_ALIGN_CENTER);
     85 		nvgFillColor(args.vg, _color);
     86 		nvgText(args.vg, _x, _y, _text, NULL);
     87 
     88 		float bounds[4];
     89 		nvgTextBounds(args.vg, _x + _w/2, _y, _text, NULL, bounds);
     90 	}
     91 };
     92 
     93 }  // namespace genrack