Skip to content

Commit 6fcbb3a

Browse files
committed
Continue UI changes
1 parent 082ec1c commit 6fcbb3a

File tree

10 files changed

+204
-139
lines changed

10 files changed

+204
-139
lines changed

Sources/GateEngine/Types/Camera.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Camera {
2020
}
2121
}
2222

23-
@usableFromInline internal var fieldOfView: FieldOfView {
23+
public var fieldOfView: FieldOfView {
2424
didSet {
2525
needsUpdateProjection = true
2626
}
@@ -88,7 +88,12 @@ public class Camera {
8888
needsUpdateTransform = false
8989
let position = Matrix4x4(position: transform.position * -1.0)
9090
let rotation = Matrix4x4(rotation: transform.rotation.conjugate)
91-
self.view = Self.viewScale * rotation * position
91+
let scale = if transform.scale == .one {
92+
Self.viewScale
93+
}else{
94+
Matrix4x4(scale: Size3(transform.scale.x, transform.scale.y, -transform.scale.z))
95+
}
96+
self.view = scale * rotation * position
9297
}
9398

9499
self.matrices = Matrices(projection: self.perspective, view: self.view)
@@ -110,27 +115,27 @@ public struct ClippingPlane {
110115
}
111116
public static let minPerspectiveNear: Float = 1 / Float(UInt8.max)
112117

113-
internal init(_ range: ClosedRange<Float>) {
118+
public init(_ range: ClosedRange<Float>) {
114119
self.near = range.lowerBound
115120
self.far = range.upperBound
116121
}
117-
internal init(_ range: Range<Float>) {
122+
public init(_ range: Range<Float>) {
118123
if range.lowerBound == 0 {
119124
self.near = range.lowerBound
120125
} else {
121126
self.near = range.lowerBound
122127
}
123128
self.far = range.upperBound - Float.leastNormalMagnitude
124129
}
125-
internal init(_ range: PartialRangeThrough<Float>) {
130+
public init(_ range: PartialRangeThrough<Float>) {
126131
self.near = Self.minPerspectiveNear
127132
self.far = range.upperBound
128133
}
129-
internal init(_ range: PartialRangeUpTo<Float>) {
134+
public init(_ range: PartialRangeUpTo<Float>) {
130135
self.near = Self.minPerspectiveNear
131136
self.far = range.upperBound - Float.leastNormalMagnitude
132137
}
133-
internal init(_ range: PartialRangeFrom<Float>) {
138+
public init(_ range: PartialRangeFrom<Float>) {
134139
self.near = range.lowerBound
135140
self.far = Float.greatestFiniteMagnitude
136141
}

Sources/GateEngine/UI/Button.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ open class Button: Control {
145145
}
146146

147147
public init(size: Size2? = nil, label: String? = nil, textColor: Color = .white, backgroundColor: Color = .blue, cornorRadius: Float? = nil, action: ((Button)->())? = nil) {
148-
super.init(size: size)
148+
super.init()
149+
150+
if let size {
151+
self.widthAnchor.constrain(to: size.width)
152+
self.heightAnchor.constrain(to: size.height)
153+
}
149154

150155
self.setBackgroundColor(backgroundColor)
151156

Sources/GateEngine/UI/GameViewController.swift

Lines changed: 22 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -73,53 +73,6 @@ public final class GameView: View {
7373
}
7474
}
7575

76-
public override func touchesBegan(_ touches: Set<Touch>) {
77-
self.gameViewController?.touchesBegan(touches)
78-
}
79-
public override func touchesMoved(_ touches: Set<Touch>) {
80-
self.gameViewController?.touchesMoved(touches)
81-
}
82-
public override func touchesEnded(_ touches: Set<Touch>) {
83-
self.gameViewController?.touchesEnded(touches)
84-
}
85-
public override func touchesCanceled(_ touches: Set<Touch>) {
86-
self.gameViewController?.touchesCanceled(touches)
87-
}
88-
89-
public override func surfaceTouchesBegan(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
90-
self.gameViewController?.surfaceTouchesBegan(touches, mouse: mouse)
91-
}
92-
public override func surfaceTouchesMoved(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
93-
self.gameViewController?.surfaceTouchesMoved(touches, mouse: mouse)
94-
}
95-
public override func surfaceTouchesEnded(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
96-
self.gameViewController?.surfaceTouchesEnded(touches, mouse: mouse)
97-
}
98-
public override func surfaceTouchesCanceled(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
99-
self.gameViewController?.surfaceTouchesCanceled(touches, mouse: mouse)
100-
}
101-
102-
public override func cursorEntered(_ cursor: Mouse) {
103-
self.gameViewController?.cursorEntered(cursor)
104-
}
105-
public override func cursorMoved(_ cursor: Mouse) {
106-
self.gameViewController?.cursorMoved(cursor)
107-
}
108-
public override func cursorExited(_ cursor: Mouse) {
109-
self.gameViewController?.cursorExited(cursor)
110-
}
111-
112-
public override func cursorButtonDown(button: MouseButton, mouse: Mouse) {
113-
self.gameViewController?.cursorButtonDown(button: button, mouse: mouse)
114-
}
115-
public override func cursorButtonUp(button: MouseButton, mouse: Mouse) {
116-
self.gameViewController?.cursorButtonUp(button: button, mouse: mouse)
117-
}
118-
119-
public override func scrolled(_ delta: Position2, isPlatformGeneratedMomentum isMomentum: Bool) {
120-
self.gameViewController?.scrolled(delta, isPlatformGeneratedMomentum: isMomentum)
121-
}
122-
12376
enum Mode {
12477
case screen
12578
case offScreen
@@ -249,7 +202,11 @@ open class GameViewController: ViewController {
249202

250203
internal override func _update(withTimePassed deltaTime: Float) async {
251204
await super._update(withTimePassed: deltaTime)
252-
self.shouldSkipRendering = (await context.shouldRenderAfterUpdate(withTimePassed: deltaTime) == false)
205+
if view.superView != nil {
206+
self.shouldSkipRendering = (await context.shouldRenderAfterUpdate(withTimePassed: deltaTime) == false)
207+
}else{
208+
self.shouldSkipRendering = true
209+
}
253210
}
254211

255212
@MainActor
@@ -260,53 +217,6 @@ open class GameViewController: ViewController {
260217
open func render(context: ECSContext, into view: GameView, withTimePassed deltaTime: Float) {
261218

262219
}
263-
264-
open func touchesBegan(_ touches: Set<Touch>) {
265-
266-
}
267-
open func touchesMoved(_ touches: Set<Touch>) {
268-
269-
}
270-
open func touchesEnded(_ touches: Set<Touch>) {
271-
272-
}
273-
open func touchesCanceled(_ touches: Set<Touch>) {
274-
275-
}
276-
277-
open func surfaceTouchesBegan(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
278-
279-
}
280-
open func surfaceTouchesMoved(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
281-
282-
}
283-
open func surfaceTouchesEnded(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
284-
285-
}
286-
open func surfaceTouchesCanceled(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
287-
288-
}
289-
290-
open func cursorEntered(_ cursor: Mouse) {
291-
292-
}
293-
open func cursorMoved(_ cursor: Mouse) {
294-
295-
}
296-
open func cursorExited(_ cursor: Mouse) {
297-
298-
}
299-
300-
open func cursorButtonDown(button: MouseButton, mouse: Mouse) {
301-
302-
}
303-
open func cursorButtonUp(button: MouseButton, mouse: Mouse) {
304-
305-
}
306-
307-
open func scrolled(_ delta: Position2, isPlatformGeneratedMomentum isMomentum: Bool) {
308-
309-
}
310220
}
311221

312222
extension GameView {
@@ -355,8 +265,23 @@ extension GameView {
355265
let p2 = Position3(dx * far, dy * far, far) * inverseView
356266

357267
return Ray3D(from: p1, toward: p2)
358-
case .orthographic(_):
359-
fatalError("Not implemented")
268+
case .orthographic(let center):
269+
let size = self.bounds.size * interfaceScale
270+
var position = position * interfaceScale
271+
switch center {
272+
case .topLeft:
273+
break
274+
case .center:
275+
position -= size / 2
276+
}
277+
278+
let x = position.x
279+
let y = position.y
280+
281+
let inverseView = camera.matricies(withViewportSize: size).view.inverse
282+
let start = Position3(x, y, -1) * inverseView
283+
284+
return Ray3D(from: start, toward: camera.transform.rotation.forward)
360285
}
361286
}
362287
}

Sources/GateEngine/UI/Label.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final class Label: View {
5555
}
5656
return _geometry
5757
}
58-
private var _size: Size2 = .zero
58+
private var _size: Size2
5959
public var size: Size2 {
6060
if needsUpdateGeometry, font.state == .ready {
6161
needsUpdateGeometry = false
@@ -145,6 +145,7 @@ public final class Label: View {
145145
self.style = style
146146
self.textColor = textColor
147147
self.textAlignment = textAlignment
148+
self._size = Size2(width: Float(fontSize), height: Float(fontSize))
148149
super.init()
149150
}
150151

Sources/GateEngine/UI/Layout.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public struct Layout {
201201
guard let targetY = relativeResolvedY(forTarget: target.view) else {continue}
202202
if target == target.view.topAnchor {
203203
computed = Value.Computed(
204-
value: targetY + vPosition.constant
204+
value: targetY - height.value + vPosition.constant
205205
)
206206
break
207207
}
@@ -348,7 +348,7 @@ public struct Layout {
348348

349349
if computed == nil {
350350
let contentWidth = view.contentSize().width
351-
if contentWidth > 0 {
351+
if contentWidth.isFinite {
352352
computed = Value<Layout.Horizontal, Layout.Size>.Computed(value: contentWidth)
353353
}
354354
}
@@ -428,6 +428,7 @@ public struct Layout {
428428
}
429429
}
430430
}
431+
431432
if computed == nil {
432433
// Subview must determine height
433434
var lowestSubviewBottom: Float? = nil
@@ -450,7 +451,7 @@ public struct Layout {
450451

451452
if computed == nil {
452453
let contentHeight = view.contentSize().height
453-
if contentHeight > 0 {
454+
if contentHeight.isFinite {
454455
computed = Value<Layout.Vertical, Layout.Size>.Computed(value: contentHeight)
455456
}
456457
}

Sources/GateEngine/UI/ScrollView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ open class ScrollView: View {
7171
return Size2(maxX, maxY)
7272
}
7373

74-
override public init(size: Size2? = nil) {
75-
super.init(size: size)
74+
override public init() {
75+
super.init()
7676
self.clipToBounds = true
7777
self.addSubview(contentView)
7878
}

Sources/GateEngine/UI/SplitViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ final class SplitViewDividerControl: Control {
1717

1818
var isDragging: Bool = false
1919

20-
override init(size: Size2? = nil) {
21-
super.init(size: size)
20+
override init() {
21+
super.init()
2222
divider.backgroundColor = .darkGray
2323
self.addSubview(divider)
2424
}
@@ -127,8 +127,8 @@ public final class SplitView: View {
127127
}
128128
}
129129

130-
public override init(size: Size2? = nil) {
131-
super.init(size: size)
130+
public override init() {
131+
super.init()
132132

133133
}
134134

Sources/GateEngine/UI/StackView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public final class StackView: View {
5353
switch axis {
5454
case .horizontal:
5555
return Size2(
56-
width: subviews.last?.frame.maxX ?? 0,
56+
width: subviews.last?.frame.maxX ?? 0,
5757
height: (subviews.sorted(by: {$0.bounds.height > $1.bounds.height}).first?.bounds.height ?? 0) + (spacing * Float(subviews.count - 1))
5858
)
5959
case .vertical:
6060
return Size2(
61-
width: (subviews.sorted(by: {$0.bounds.width > $1.bounds.width}).first?.bounds.width ?? 0) + (spacing * Float(subviews.count - 1)),
61+
width: (subviews.sorted(by: {$0.bounds.width > $1.bounds.width}).first?.bounds.width ?? 0) + (spacing * Float(subviews.count - 1)),
6262
height: subviews.last?.frame.maxY ?? 0
6363
)
6464
}
@@ -86,7 +86,7 @@ public final class StackView: View {
8686
subView.centerYAnchor.constrain(to: self.centerYAnchor)
8787
previousView = subView
8888
}
89-
subviews.last?.trailingAnchor.constrain(to: self.trailingAnchor)
89+
previousView.trailingAnchor.constrain(to: self.trailingAnchor)
9090
}
9191
case .vertical:
9292
switch distribution {
@@ -104,7 +104,7 @@ public final class StackView: View {
104104
subView.trailingAnchor.constrain(to: self.trailingAnchor)
105105
previousView = subView
106106
}
107-
subviews.last?.bottomAnchor.constrain(to: self.bottomAnchor)
107+
previousView.bottomAnchor.constrain(to: self.bottomAnchor)
108108
}
109109
}
110110
}

0 commit comments

Comments
 (0)