1 /// 2 module nanogui.experimental.treeview; 3 4 /* 5 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>. 6 The widget drawing code is based on the NanoVG demo application 7 by Mikko Mononen. 8 9 All rights reserved. Use of this source code is governed by a 10 BSD-style license that can be found in the LICENSE.txt file. 11 */ 12 13 import nanogui.widget; 14 import nanogui.common : MouseButton, Vector2f, Vector2i; 15 import nanogui.experimental.utils : Model; 16 17 /** 18 * Tree view widget. 19 */ 20 class TreeView(Data) : Widget 21 { 22 public: 23 24 enum modelHasCollapsed = is(typeof(Model!Data.collapsed) == bool); 25 26 /** 27 * Adds a TreeView to the specified `parent`. 28 * 29 * Params: 30 * parent = The Widget to add this TreeView to. 31 * caption = The caption text of the TreeView (default `"Untitled"`). 32 * callback = If provided, the callback to execute when the TreeView is 33 * checked or unchecked. Default parameter function does nothing. See 34 * `nanogui.TreeView.mPushed` for the difference between "pushed" 35 * and "checked". 36 */ 37 this(Widget parent, string caption, Data data, void delegate(bool) callback) 38 { 39 super(parent); 40 mCaption = caption; 41 static if (modelHasCollapsed) 42 { 43 mPushed = false; 44 mChecked = false; 45 mCallback = callback; 46 } 47 _data = data; 48 _model = makeModel(_data); 49 import nanogui.experimental.utils : MeasuringVisitor; 50 auto v = MeasuringVisitor(fontSize); 51 _model.visitForward(_data, v); 52 } 53 54 /// The caption of this TreeView. 55 final string caption() const { return mCaption; } 56 57 /// Sets the caption of this TreeView. 58 final void caption(string caption) { mCaption = caption; } 59 60 static if (modelHasCollapsed) 61 { 62 /// Whether or not this TreeView is currently checked. 63 final bool checked() const { return _model.collapsed; } 64 65 /// Sets whether or not this TreeView is currently checked. 66 final void checked(bool checked) { _model.collapsed = checked; } 67 68 /// Whether or not this TreeView is currently pushed. See `nanogui.TreeView.mPushed`. 69 final bool pushed() const { return mPushed; } 70 71 /// Sets whether or not this TreeView is currently pushed. See `nanogui.TreeView.mPushed`. 72 final void pushed(bool pushed) { mPushed = pushed; } 73 74 /// Returns the current callback of this TreeView. 75 final void delegate(bool) callback() const { return mCallback; } 76 77 /// Sets the callback to be executed when this TreeView is checked / unchecked. 78 final void callback(void delegate(bool) callback) { mCallback = callback; } 79 } 80 81 /** 82 * The mouse button callback will return `true` when all three conditions are met: 83 * 84 * 1. This TreeView is "enabled" (see `nanogui.Widget.mEnabled`). 85 * 2. `p` is inside this TreeView. 86 * 3. `button` is `MouseButton.Left`. 87 * 88 * Since a mouse button event is issued for both when the mouse is pressed, as well 89 * as released, this function sets `nanogui.TreeView.mPushed` to `true` when 90 * parameter `down == true`. When the second event (`down == false`) is fired, 91 * `nanogui.TreeView.mChecked` is inverted and `nanogui.TreeView.mCallback` 92 * is called. 93 * 94 * That is, the callback provided is only called when the mouse button is released, 95 * **and** the click location remains within the TreeView boundaries. If the user 96 * clicks on the TreeView and releases away from the bounds of the TreeView, 97 * `nanogui.TreeView.mPushed` is simply set back to `false`. 98 */ 99 override bool mouseButtonEvent(Vector2i p, MouseButton button, bool down, int modifiers) 100 { 101 if (!mEnabled) 102 return false; 103 104 static if (modelHasCollapsed) 105 { 106 import nanogui.experimental.utils : isPointInRect; 107 const rect_size = Vector2i(mSize.x, !_model.collapsed ? cast(int) (fontSize() * 1.3f) : mSize.y); 108 109 if (button == MouseButton.Left) 110 { 111 import nanogui.experimental.utils : setPropertyByTreePath, getPropertyByTreePath; 112 if (!down && tree_path.value.length) 113 { 114 const value = getPropertyByTreePath!("collapsed", bool)(_data, _model, tree_path.value[]); 115 if (!value.isNull) 116 { 117 setPropertyByTreePath!"collapsed"(_data, _model, tree_path.value[], !value.get); 118 import nanogui.experimental.utils : MeasuringVisitor; 119 auto mv = MeasuringVisitor(fontSize); 120 _model.visitForward(_data, mv); 121 screen.needToPerfomLayout = true; 122 } 123 } 124 if (!isPointInRect(mPos, rect_size, p)) 125 return false; 126 if (down) 127 { 128 mPushed = true; 129 } 130 else if (mPushed) 131 { 132 mChecked = !mChecked; 133 if (mCallback) 134 mCallback(mChecked); 135 mPushed = false; 136 } 137 return true; 138 } 139 } 140 141 return super.mouseButtonEvent(p, button, down, modifiers); 142 } 143 144 /// The preferred size of this TreeView. 145 override Vector2i preferredSize(NanoContext ctx) const 146 { 147 if (mFixedSize != Vector2i()) 148 return mFixedSize; 149 ctx.fontSize(fontSize()); 150 ctx.fontFace("sans"); 151 float[4] bounds; 152 153 return cast(Vector2i) Vector2f( 154 (ctx.textBounds(0, 0, mCaption, bounds[]) + 155 1.8f * fontSize()), 156 _model.size); 157 } 158 159 /// Draws this TreeView. 160 override void draw(ref NanoContext ctx) 161 { 162 // do not call super.draw() because we do custom drawing 163 164 //ctx.fontSize(theme.mButtonFontSize); 165 //ctx.fontFace("sans-bold"); 166 167 ctx.theme = theme; 168 ctx.size = Vector2f(size.x, ctx.fontSize); 169 ctx.position = mPos; 170 171 //ctx.mouse -= mPos; 172 //scope(exit) ctx.mouse += mPos; 173 174 auto renderer = RenderingVisitor(ctx); 175 renderer.destination = ctx.position.y + size.y; 176 import nanogui.layout : Orientation; 177 renderer.ctx.orientation = Orientation.Vertical; 178 _model.visitForward(_data, renderer); 179 tree_path = renderer.selected_item; 180 } 181 182 // // Saves this TreeView to the specified Serializer. 183 //override void save(Serializer &s) const; 184 185 // // Loads the state of the specified Serializer to this TreeView. 186 //override bool load(Serializer &s); 187 188 protected: 189 190 import nanogui.experimental.utils : makeModel, visit, visitForward, TreePath; 191 192 /// The caption text of this TreeView. 193 string mCaption; 194 195 static if (modelHasCollapsed) 196 { 197 /** 198 * Internal tracking variable to distinguish between mouse click and release. 199 * `nanogui.TreeView.mCallback` is only called upon release. See 200 * `nanogui.TreeView.mouseButtonEvent` for specific conditions. 201 */ 202 bool mPushed; 203 204 bool mChecked() const 205 { 206 static if (is(typeof(_model.collapsed) == bool)) 207 return !_model.collapsed; 208 else 209 return false; 210 } 211 212 auto mChecked(bool v) 213 { 214 static if (is(typeof(_model.collapsed) == bool)) 215 if (_model.collapsed == v) 216 { 217 _model.collapsed = !v; 218 import nanogui.experimental.utils : MeasuringVisitor; 219 auto mv = MeasuringVisitor(fontSize); 220 _model.visitForward(_data, mv); 221 screen.needToPerfomLayout = true; 222 } 223 } 224 225 /// The function to execute when `nanogui.TreeView.mChecked` is changed. 226 void delegate(bool) mCallback; 227 } 228 229 Data _data; 230 typeof(makeModel(_data)) _model; 231 232 // sequence of indices to get access to current element of current treeview 233 TreePath tree_path; 234 } 235 236 private struct RenderingVisitor 237 { 238 import nanogui.experimental.utils : drawItem, indent, unindent, TreePath; 239 import auxil.model; 240 241 NanoContext ctx; 242 DefaultVisitorImpl!(SizeEnabled.no, TreePathEnabled.yes) default_visitor; 243 alias default_visitor this; 244 245 TreePath selected_item; 246 float finish; 247 248 bool complete() 249 { 250 return ctx.position.y > finish; 251 } 252 253 void indent() 254 { 255 ctx.indent; 256 } 257 258 void unindent() 259 { 260 ctx.unindent; 261 } 262 263 void enterNode(Order order, Data, Model)(ref const(Data) data, ref Model model) 264 { 265 ctx.save; 266 scope(exit) ctx.restore; 267 268 { 269 // background for icon 270 NVGPaint bg = ctx.boxGradient( 271 ctx.position.x + 1.5f, ctx.position.y + 1.5f, 272 ctx.size[ctx.orientation] - 2.0f, ctx.size[ctx.orientation] - 2.0f, 3, 3, 273 true/*pushed*/ ? Color(0, 0, 0, 100) : Color(0, 0, 0, 32), 274 Color(0, 0, 0, 180) 275 ); 276 277 ctx.beginPath; 278 ctx.roundedRect(ctx.position.x + 1.0f, ctx.position.y + 1.0f, 279 ctx.size[ctx.orientation] - 2.0f, ctx.size[ctx.orientation] - 2.0f, 3); 280 ctx.fillPaint(bg); 281 ctx.fill; 282 } 283 284 { 285 // icon 286 ctx.fontSize(ctx.size.y); 287 ctx.fontFace("icons"); 288 ctx.fillColor(model.enabled ? ctx.theme.mIconColor 289 : ctx.theme.mDisabledTextColor); 290 NVGTextAlign algn; 291 algn.center = true; 292 algn.middle = true; 293 ctx.textAlign(algn); 294 295 import nanogui.entypo : Entypo; 296 int axis2 = (cast(int)ctx.orientation+1)%2; 297 const old = ctx.size[axis2]; 298 ctx.size[axis2] = ctx.size[ctx.orientation]; // icon has width equals to its height 299 dchar symb = model.collapsed ? Entypo.ICON_CHEVRON_RIGHT : 300 Entypo.ICON_CHEVRON_DOWN; 301 if (drawItem(ctx, ctx.size[ctx.orientation], [symb])) 302 selected_item = tree_path; 303 ctx.size[axis2] = old; // restore full width 304 ctx.position[ctx.orientation] -= ctx.size[ctx.orientation]; 305 } 306 307 { 308 // Caption 309 ctx.position.x += 1.6f * ctx.size.y; 310 scope(exit) ctx.position.x -= 1.6f * ctx.size.y; 311 ctx.fontSize(ctx.size.y); 312 ctx.fontFace("sans"); 313 ctx.fillColor(model.enabled ? ctx.theme.mTextColor : ctx.theme.mDisabledTextColor); 314 if (drawItem(ctx, ctx.size.y, Data.stringof)) 315 selected_item = tree_path; 316 } 317 } 318 319 void processLeaf(Order order, Data, Model)(ref const(Data) data, ref Model model) 320 { 321 ctx.fontSize(ctx.size.y); 322 ctx.fontFace("sans"); 323 ctx.fillColor(ctx.theme.mTextColor); 324 if (drawItem(ctx, cast(int) ctx.size[ctx.orientation], data)) 325 selected_item = tree_path; 326 } 327 }