Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.13 KB | None | 0 0
  1.  
  2. class Button : TaggedObject, Drawable, Updatable {
  3.   vec2i pos; string text;
  4.   vec2i to;
  5.   float delegate(TransitionMode) dg;
  6.   vec4f fill;
  7.   void delegate() onClick;
  8.   void init(string s, vec2i p, void delegate() dg) {
  9.     (text, pos, onClick) = (s, p, dg);
  10.     this.dg = genTransition(0, 1, transferTime => 0.3);
  11.     fill = vec4f(0.2, 0.4, 0.8, 0.7);
  12.   }
  13.   string ident() return "Button";
  14.   Tag offers(string s) {
  15.     if s == "Button" || s == "Drawable" || s == "Updatable" return this;
  16.     return super.offers s;
  17.   }
  18.   void draw() {
  19.     auto leaf = parseText text;
  20.     auto h = printAt(overlay, pos, leaf, width => 90, radius => 7, fill => fill);
  21.     to = pos + vec2i(90, h);
  22.   }
  23.   void update(void delegate(Drawable) addDrawable) {
  24.     bool inside, active;
  25.     if (mouse-pos.(x >= pos.x && y >= pos.y && x < to.x && y < to.y)) {
  26.       inside = true;
  27.       if mouse-released onClick();
  28.       if mouse-pressed active = true;
  29.     }
  30.     auto factor = dg([TransitionMode.Down, TransitionMode.Up][inside]);
  31.     fill = vec4f(0.2, 0.4, 0.8, 0.6) * (1 - factor)
  32.          + vec4f(0.2, 0.4, 0.8, [float:0.8, 1][active]) * factor;
  33.   }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement