1 module glui.size_lock; 2 3 import raylib; 4 import std.algorithm; 5 6 import glui.node; 7 import glui.utils; 8 import glui.style; 9 10 11 12 @safe: 13 14 15 /// Create a size-locked node 16 alias sizeLock(alias T) = simpleConstructor!(GluiSizeLock, T); 17 18 SizeLimit sizeLimit(size_t x, size_t y) { 19 20 return SizeLimit(x, y); 21 22 } 23 24 SizeLimit sizeLimitX(size_t x) { 25 26 return SizeLimit(x, 0); 27 28 } 29 30 SizeLimit sizeLimitY(size_t y) { 31 32 return SizeLimit(0, y); 33 34 } 35 36 struct SizeLimit { 37 38 size_t x; 39 size_t y; 40 41 } 42 43 /// Limit the size of a given node (as if imposed by parent) while attempting to copy the behavior of a fill-aligned 44 /// node. 45 class GluiSizeLock(T : GluiNode) : T { 46 47 mixin DefineStyles; 48 49 /// The maximum size of this node. 50 /// If a value is `0`, it will not be limited. 51 SizeLimit limit; 52 53 static foreach (i; 0..BasicNodeParamLength) { 54 55 this(T...)(BasicNodeParam!i params, SizeLimit limit, T args) { 56 57 super(params, args); 58 this.limit = limit; 59 60 } 61 62 } 63 64 override void resizeImpl(Vector2 space) { 65 66 // Limit available space 67 if (limit.x != 0) space.x = min(space.x, limit.x); 68 if (limit.y != 0) space.y = min(space.y, limit.y); 69 70 // Resize 71 super.resizeImpl(space); 72 73 // Apply the limit to the resulting value; fill in remaining space if available 74 if (limit.x != 0) minSize.x = max(space.x, min(limit.x, minSize.x)); 75 if (limit.y != 0) minSize.y = max(space.y, min(limit.y, minSize.y)); 76 77 } 78 79 }