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.tooltipmanager; 8 9 import anchovy.gui; 10 11 struct TooltipManager 12 { 13 private: 14 GuiContext _context; 15 16 Widget _tooltip; 17 18 public: 19 20 @disable this(); 21 22 this(GuiContext context) 23 { 24 _context = context; 25 } 26 27 void onWidgetHovered(Widget widget) 28 { 29 if (widget is null) 30 { 31 hideTooltip(); 32 33 return; 34 } 35 36 if (widget.hasProperty("tooltip")) 37 { 38 showTooltip(widget.coercePropertyAs!("tooltip", string), 39 _context.eventDispatcher.lastPointerPosition() + ivec2(0, 20)); 40 } 41 else 42 { 43 hideTooltip(); 44 } 45 } 46 47 void showTooltip(string text, ivec2 pos) 48 { 49 if (_tooltip is null) 50 { 51 _tooltip = _context.getWidgetById("tooltip"); 52 if (_tooltip is null) 53 { 54 _tooltip = _context.createWidget("tooltip"); 55 } 56 } 57 58 _tooltip["text"] = text; 59 _tooltip["position"] = pos; 60 _context.overlay.addChild(_tooltip); 61 } 62 63 void hideTooltip() 64 { 65 _context.overlay.removeChild(_tooltip); 66 } 67 }