1 /** 2 Button 3 4 Copyright: (c) Enalye 2017 5 License: Zlib 6 Authors: Enalye 7 */ 8 9 module atelier.ui.button; 10 11 import std.conv : to; 12 import atelier.core, atelier.render, atelier.common; 13 import atelier.ui.gui_element, atelier.ui.label; 14 15 /// Simple gui that trigger its callback when onSubmit() is fired (like when you click on it). 16 class Button : GuiElement { 17 /// Function callback triggered when *onSubmit* is called. 18 void function() onClick; 19 20 override void onSubmit() { 21 if (isLocked) 22 return; 23 if (onClick !is null) 24 onClick(); 25 triggerCallback(); 26 } 27 } 28 29 /// Button with a label. 30 class TextButton : Button { 31 /// The text of the button. 32 Label label; 33 34 @property { 35 alias color = label.color; 36 alias text = label.text; 37 } 38 39 /// Ctor 40 this(string text, Font font = getDefaultFont()) { 41 label = new Label(text, font); 42 label.setAlign(GuiAlignX.center, GuiAlignY.center); 43 size = label.size; 44 appendChild(label); 45 } 46 47 override void draw() { 48 if (isLocked) 49 drawFilledRect(origin, size, Color.white * 0.055f); 50 else if (isSelected) 51 drawFilledRect(origin, size, Color.white * 0.4f); 52 else if (isHovered) 53 drawFilledRect(origin, size, Color.white * 0.25f); 54 else 55 drawFilledRect(origin, size, Color.white * 0.15f); 56 } 57 }