Skip to content

Commit c3c8390

Browse files
committed
actually, nevermind; release v1.0
1 parent 2675a08 commit c3c8390

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
# rectcut.lua
2-
an implementation of the RectCut layouting system for Lua
2+
3+
an implementation of the [RectCut layouting system](https://halt.software/p/rectcut-for-dead-simple-ui-layouts) for Lua.
4+
5+
## usage
6+
7+
clone this repository as `rectcut` wherever you want, then `require 'rectcut'`.
8+
this will provide you with a Rect class, which you can then instantiate by calling or by the functions `new` or `fromXYWH`.
9+
10+
## example
11+
12+
these examples are adapted from the article linked previously.
13+
14+
### toolbar layout
15+
16+
```lua
17+
local Rect = require 'rectcut'
18+
19+
local layout = Rect(0, 0, 180, 16)
20+
21+
local r1 = layout:cut_left(16)
22+
local r2 = layout:cut_left(16)
23+
local r3 = layout:cut_left(16)
24+
25+
local r4 = layout:cut_right(16)
26+
local r5 = layout:cut_right(16)
27+
```
28+
29+
### two-panel application
30+
31+
```lua
32+
local top = layout:cut_top(16)
33+
local button_close = top:cut_right(16)
34+
local button_maximize = top:cut_right(16)
35+
local button_minimize = top:cut_right(16)
36+
local title = top
37+
38+
local bottom = layout:cut_bottom(16)
39+
40+
local panel_left = layout:cut_left(w/2)
41+
local panel_right = layout
42+
```
43+
44+
## license
45+
46+
this module is free software; you can redistribute it and/or modify it under the terms of the MIT License. See [LICENSE](./LICENSE) for details.

init.lua

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local min, max = math.min, math.max
99
---@field public maxY number
1010
---@overload fun(minX: number, minY: number, maxX: number, maxY: number): Rect
1111
---@overload fun(r: {minX: number, minY: number, maxX: number, maxY: number}): Rect
12-
local Rect = {}
12+
local Rect = { _version = "1.0" }
1313
Rect.__index = Rect
1414

1515
---Creates a new Rect. Equivalent to calling the class: `Rect(...)`.
@@ -68,6 +68,16 @@ function Rect:xywh()
6868
self.minY + (self.maxY - self.minY)
6969
end
7070

71+
---A variant of the `get_*` methods that takes in the side to get from.
72+
---@param side "left"|"right"|"top"|"bottom"
73+
---@param a number
74+
---@return Rect
75+
function Rect:get(side, a)
76+
local f = self["get_" .. side]
77+
assert(f, "no such side: " .. side)
78+
return f(self, a)
79+
end
80+
7181
---Same as `cut_left`, except it keeps the Rect intact.
7282
---@param a number
7383
---@return Rect
@@ -108,6 +118,16 @@ function Rect:get_bottom(a)
108118
)
109119
end
110120

121+
---A variant of the `cut_*` methods that takes in the side to cut from.
122+
---@param side "left"|"right"|"top"|"bottom"
123+
---@param a number
124+
---@return Rect
125+
function Rect:cut(side, a)
126+
local f = self["cut_" .. side]
127+
assert(f, "no such side: " .. side)
128+
return f(self, a)
129+
end
130+
111131
---Cuts `a` from the left side of the Rect, and returns the cut part.
112132
---@param a number
113133
---@return Rect
@@ -164,6 +184,16 @@ function Rect:add_top(a) return Rect.new(self.minX, self.minY - a, self.maxX, se
164184
---@return Rect
165185
function Rect:add_bottom(a) return Rect.new(self.minX, self.minY, self.maxX, self.maxY + a) end
166186

187+
---A variant of the `add_*` methods that takes in the side to add from.
188+
---@param side "left"|"right"|"top"|"bottom"
189+
---@param a number
190+
---@return Rect
191+
function Rect:add(side, a)
192+
local f = self["add_" .. side]
193+
assert(f, "no such side: " .. side)
194+
return f(self, a)
195+
end
196+
167197
---Returns an extended version of this Rect.
168198
---@param a number
169199
---@return Rect

0 commit comments

Comments
 (0)