Skip to content

Commit 28f4984

Browse files
committed
- Split LayoutComponents off in to its own file
- Created "LayoutComponents" subdirectory, as we may have a lot of these (labels, colorbars, plot markers, etc) - Created "Color" subdirectory, as this PR adds lots of Color infrastructure and there may be more to come (more built-in colormaps, colour schemes, etc)
1 parent 9a7b8d0 commit 28f4984

File tree

6 files changed

+77
-75
lines changed

6 files changed

+77
-75
lines changed
File renamed without changes.

Sources/SwiftPlot/GraphLayout.swift

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,6 @@ public enum LegendIcon {
55
case shape(ScatterPlotSeriesOptions.ScatterPattern, Color)
66
}
77

8-
public protocol LayoutComponent {
9-
10-
/// Returns the minimum size required to display this element.
11-
///
12-
/// - parameters:
13-
/// - edge: The edge on which the element will be displayed.
14-
/// - renderer: The renderer which will be used to draw the element. Helpful for measuring text.
15-
/// - note: The return value's `width` and `height` always correspond to x and y distances, respectively.
16-
/// Rectangular elements will want to swap their horizontal widths and heights when displayed on vertical edges.
17-
///
18-
func measure(edge: RectEdge, _ renderer: Renderer) -> Size
19-
20-
/// Draws the element in the given `Rect`.
21-
///
22-
/// - parameters:
23-
/// - rect: The region to draw in.
24-
/// - measuredSize: The minimum size calculated by the last call to `measure`. Helpful if the size is expensive to calculate.
25-
/// - edge: The edge on which the element will be displayed.
26-
/// - renderer: The renderer to use when drawing the element.
27-
///
28-
func draw(_ rect: Rect, measuredSize: Size, edge: RectEdge, renderer: Renderer)
29-
}
30-
31-
/// A `LayoutComponent` wrapper which adds padding to an internal `LayoutComponent`
32-
///
33-
private struct _Padded<T: LayoutComponent>: LayoutComponent {
34-
var base: T
35-
var padding: EdgeComponents<Float> = .zero
36-
37-
func measure(edge: RectEdge, _ renderer: Renderer) -> Size {
38-
var size = base.measure(edge: edge, renderer)
39-
size.width += padding.left + padding.right
40-
size.height += padding.top + padding.bottom
41-
return size
42-
}
43-
func draw(_ rect: Rect, measuredSize: Size, edge: RectEdge, renderer: Renderer) {
44-
var adjustedMeasuredSize = measuredSize
45-
adjustedMeasuredSize.width -= padding.left + padding.right
46-
adjustedMeasuredSize.height -= padding.top + padding.bottom
47-
let adjustedRect = rect.inset(by: padding)
48-
base.draw(adjustedRect, measuredSize: adjustedMeasuredSize, edge: edge, renderer: renderer)
49-
}
50-
}
51-
extension LayoutComponent {
52-
53-
/// Returns a new `LayoutComponent` which adds the given padding to this `LayoutComponent`.
54-
///
55-
public func padding(_ padding: EdgeComponents<Float>) -> LayoutComponent {
56-
return _Padded(base: self, padding: padding)
57-
}
58-
}
59-
60-
public struct Label: LayoutComponent {
61-
var text: String = ""
62-
var size: Float = 12
63-
var color: Color = .black
64-
65-
public func measure(edge: RectEdge, _ renderer: Renderer) -> Size {
66-
let hSize = renderer.getTextLayoutSize(text: text, textSize: size)
67-
return edge.isHorizontal ? hSize : hSize.swappingComponents()
68-
}
69-
public func draw(_ rect: Rect, measuredSize: Size, edge: RectEdge, renderer: Renderer) {
70-
var origin = rect.center + Point(-measuredSize.width/2, -measuredSize.height/2)
71-
let angle: Float
72-
if edge.isHorizontal {
73-
angle = 0
74-
} else {
75-
angle = 90
76-
origin += Point(rect.width, 0)
77-
}
78-
renderer.drawText(text: text, location: origin, textSize: size,
79-
color: color, strokeWidth: 1.2, angle: angle)
80-
}
81-
}
82-
838
/// A container which has a value at each `RectEdge`.
849
public struct EdgeComponents<T> {
8510
public var left: T
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
public struct Label: LayoutComponent {
3+
var text: String = ""
4+
var size: Float = 12
5+
var color: Color = .black
6+
7+
public func measure(edge: RectEdge, _ renderer: Renderer) -> Size {
8+
let hSize = renderer.getTextLayoutSize(text: text, textSize: size)
9+
return edge.isHorizontal ? hSize : hSize.swappingComponents()
10+
}
11+
public func draw(_ rect: Rect, measuredSize: Size, edge: RectEdge, renderer: Renderer) {
12+
var origin = rect.center + Point(-measuredSize.width/2, -measuredSize.height/2)
13+
let angle: Float
14+
if edge.isHorizontal {
15+
angle = 0
16+
} else {
17+
angle = 90
18+
origin += Point(rect.width, 0)
19+
}
20+
renderer.drawText(text: text, location: origin, textSize: size,
21+
color: color, strokeWidth: 1.2, angle: angle)
22+
}
23+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/// A visual element which occupies space in a layout object.
3+
///
4+
public protocol LayoutComponent {
5+
6+
/// Returns the minimum size required to display this element.
7+
///
8+
/// - parameters:
9+
/// - edge: The edge on which the element will be displayed.
10+
/// - renderer: The renderer which will be used to draw the element. Helpful for measuring text.
11+
/// - note: The return value's `width` and `height` always correspond to x and y distances, respectively.
12+
/// Rectangular elements will want to swap their horizontal widths and heights when displayed on vertical edges.
13+
///
14+
func measure(edge: RectEdge, _ renderer: Renderer) -> Size
15+
16+
/// Draws the element in the given `Rect`.
17+
///
18+
/// - parameters:
19+
/// - rect: The region to draw in.
20+
/// - measuredSize: The minimum size calculated by the last call to `measure`. Helpful if the size is expensive to calculate.
21+
/// - edge: The edge on which the element will be displayed.
22+
/// - renderer: The renderer to use when drawing the element.
23+
///
24+
func draw(_ rect: Rect, measuredSize: Size, edge: RectEdge, renderer: Renderer)
25+
}
26+
27+
/// A `LayoutComponent` wrapper which adds padding to an internal `LayoutComponent`
28+
///
29+
private struct _Padded<T: LayoutComponent>: LayoutComponent {
30+
var base: T
31+
var padding: EdgeComponents<Float> = .zero
32+
33+
func measure(edge: RectEdge, _ renderer: Renderer) -> Size {
34+
var size = base.measure(edge: edge, renderer)
35+
size.width += padding.left + padding.right
36+
size.height += padding.top + padding.bottom
37+
return size
38+
}
39+
func draw(_ rect: Rect, measuredSize: Size, edge: RectEdge, renderer: Renderer) {
40+
var adjustedMeasuredSize = measuredSize
41+
adjustedMeasuredSize.width -= padding.left + padding.right
42+
adjustedMeasuredSize.height -= padding.top + padding.bottom
43+
let adjustedRect = rect.inset(by: padding)
44+
base.draw(adjustedRect, measuredSize: adjustedMeasuredSize, edge: edge, renderer: renderer)
45+
}
46+
}
47+
extension LayoutComponent {
48+
49+
/// Returns a new `LayoutComponent` which adds the given padding to this `LayoutComponent`.
50+
///
51+
public func padding(_ padding: EdgeComponents<Float>) -> LayoutComponent {
52+
return _Padded(base: self, padding: padding)
53+
}
54+
}

0 commit comments

Comments
 (0)