1 /** 2 Copyright: Copyright (c) 2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.behaviors.checkbehavior; 8 9 import anchovy.gui; 10 import anchovy.gui.interfaces.iwidgetbehavior; 11 12 //version = Check_debug; 13 14 15 class CheckBehavior : IWidgetBehavior 16 { 17 bool isHovered; 18 bool isPressed; 19 Widget _widget; 20 21 public: 22 23 override void attachPropertiesTo(Widget widget) 24 { 25 widget.setProperty!("isChecked", bool) = false; 26 } 27 28 override void attachTo(Widget widget) 29 { 30 version(Check_debug) writeln("attachTo ", widget["name"], " ", widget["type"], " ", widget["isChecked"]); 31 _widget = widget; 32 33 widget.addEventHandler(&onClick); 34 widget.addEventHandler(&pointerPressed); 35 widget.addEventHandler(&pointerReleased); 36 widget.addEventHandler(&pointerMoved); 37 widget.addEventHandler(&pointerEntered); 38 widget.addEventHandler(&pointerLeaved); 39 40 isHovered = false; 41 isPressed = false; 42 43 widget.property("isChecked").valueChanged.connect( (FlexibleObject a, Variant b){updateState();} ); 44 45 updateState(); 46 } 47 48 bool onClick(Widget widget, PointerClickEvent event) 49 { 50 widget.setProperty!"isChecked"(!widget.getPropertyAs!("isChecked", bool)); 51 52 version(Check_debug) writeln("onClick"); 53 54 return true; 55 } 56 57 bool pointerPressed(Widget widget, PointerPressEvent event) 58 { 59 if (event.button == PointerButton.PB_LEFT) 60 { 61 isPressed = true; 62 } 63 64 updateState(); 65 66 version(Check_debug) writeln("pointerPressed"); 67 68 return true; 69 } 70 71 bool pointerReleased(Widget widget, PointerReleaseEvent event) 72 { 73 if (event.button == PointerButton.PB_LEFT) 74 { 75 isHovered = true; 76 isPressed = false; 77 } 78 79 updateState(); 80 version(Check_debug) writeln("pointerReleased"); 81 82 return true; 83 } 84 85 bool pointerMoved(Widget widget, PointerMoveEvent event) 86 { 87 return true; 88 } 89 90 bool pointerEntered(Widget widget, PointerEnterEvent event) 91 { 92 if (event.context.eventDispatcher.pressedWidget is this) 93 { 94 isPressed = true; 95 version(Check_debug) writeln("pressed"); 96 } 97 else 98 { 99 isHovered = true; 100 version(Check_debug) writeln("pointerEntered hovered"); 101 } 102 103 updateState(); 104 return true; 105 } 106 107 bool pointerLeaved(Widget widget, PointerLeaveEvent event) 108 { 109 isHovered = false; 110 111 version(Check_debug) writefln("pointerLeaved isHovered %s", isHovered); 112 updateState(); 113 114 return true; 115 } 116 117 // normal_unchecked == normal 118 static const string[6] stateStrings = ["normal", "normal_checked", "hovered_unchecked", 119 "hovered_checked", "pressed_unchecked", "pressed_checked"]; 120 121 void updateState() 122 { 123 version(Check_debug) writefln("%s %s isHovered %s", _widget["name"], _widget["type"], isHovered); 124 125 uint checked = _widget.getPropertyAs!("isChecked", bool) ? 1 : 0; 126 uint hovered = isHovered ? 2 : 0; 127 if (isPressed) hovered = 4; 128 129 version(Check_debug) writefln("checked %s hovered %s pressed %s %s", _widget.getPropertyAs!("isChecked", bool) 130 , hovered, isPressed, stateStrings[checked | hovered]); 131 132 _widget.setProperty!"state"(stateStrings[checked | hovered]); 133 } 134 }