1 // Written in the D programming language. 2 /++ 3 + Authors: KanzakiKino 4 + Copyright: KanzakiKino 2018 5 + License: LGPL-3.0 6 ++/ 7 module w4d.widget.button; 8 import w4d.parser.colorset, 9 w4d.style.rect, 10 w4d.style.scalar, 11 w4d.task.window, 12 w4d.widget.text, 13 w4d.event; 14 import g4d.glfw.cursor; 15 import gl3n.linalg; 16 17 /// A handler that handles pressing buttons. 18 alias ButtonPressHandler = EventHandler!( void ); 19 20 /// A widget of button. 21 class ButtonWidget : TextWidget 22 { 23 /// 24 ButtonPressHandler onButtonPress; 25 26 /// 27 override bool handleMouseButton ( MouseButton btn, bool status, vec2 pos ) 28 { 29 if ( super.handleMouseButton( btn, status, pos ) ) { 30 return true; 31 } 32 if ( !style.isPointInside(pos) ) { 33 return false; 34 } 35 if ( btn == MouseButton.Left && !status ) { 36 handleButtonPress(); 37 return true; 38 } 39 return false; 40 } 41 42 /// 43 void handleButtonPress () 44 { 45 onButtonPress.call(); 46 } 47 48 /// 49 override @property const(Cursor) cursor () 50 { 51 return Cursor.Hand; 52 } 53 54 /// 55 this () 56 { 57 super(); 58 textOriginRate = vec2(0.5,0.5); 59 textPosRate = vec2(0.5,0.5); 60 61 parseColorSetsFromFile!"colorset/button.yaml"( style ); 62 style.box.margins = Rect(1.mm); 63 style.box.paddings = Rect(2.mm); 64 style.box.borderWidth = Rect(1.pixel); 65 } 66 67 /// 68 override @property bool trackable () { return true; } 69 /// 70 override @property bool focusable () { return true; } 71 }