Skip to content

Commit

Permalink
fix render context
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectralDragon committed Jun 14, 2024
1 parent bf408ca commit 5b0f852
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.1.1
7.1.2
4 changes: 2 additions & 2 deletions Sources/AdaEditor/AdaEditorApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import AdaEngine
struct AdaEditorApp: App {

var scene: some AppScene {
GameAppScene {
UIViewTestScene()
GUIAppScene {
EditorWindow()
}
.windowMode(.windowed)
.windowTitle("AdaEngine")
Expand Down
35 changes: 22 additions & 13 deletions Sources/AdaEditor/UI/EditorWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ class EditorWindow: UIWindow {
override func windowDidReady() {
self.title = "Ada Editor"

self.canDraw = true
//
// let blueView = View(frame: Rect(origin: .zero, size: Size(width: 400, height: 400)))
// blueView.backgroundColor = .blue
//
// self.backgroundColor = .white
//
// let redView = View(frame: Rect(origin: Point(x: 400, y: 0), size: Size(width: 400, height: 400)))
// redView.backgroundColor = .red.opacity(0.3)
//
// self.addSubview(blueView)
// self.addSubview(redView)
self.backgroundColor = .white

let blueView = UIView(frame: Rect(origin: Point(x: 0, y: 0), size: Size(width: 50, height: 50)))
blueView.backgroundColor = .blue

let redView = UIView(frame: Rect(origin: Point(x: 100, y: 0), size: Size(width: 50, height: 50)))
// redView.zIndex = 1
redView.backgroundColor = .red

self.addSubview(blueView)
self.addSubview(redView)
}

override func draw(in rect: Rect, with context: GUIRenderContext) {
super.draw(in: rect, with: context)
print("Did render", rect)

var count = Int(rect.width / 50)

var x: Float = 0

for i in 0..<count {
context.drawRect(.init(x: x, y: 50, width: 50, height: 50), color: Color.random())
x += 50
}

}
}
11 changes: 4 additions & 7 deletions Sources/AdaEngine/Rendering/GUIRenderContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ final public class GUIRenderContext {
}
}

private var screenRect: Rect = .zero

private var currentDrawContext: Renderer2D.DrawContext?

internal func beginDraw(in rect: Rect) {
internal func beginDraw(in size: Size, scaleFactor: Float) {
let view = Transform3D.orthographic(
left: 0,
right: screenRect.width,
right: size.width / scaleFactor,
top: 0,
bottom: screenRect.height,
bottom: -size.height / scaleFactor,
zNear: -1,
zFar: 1
)
Expand Down Expand Up @@ -104,8 +102,7 @@ final public class GUIRenderContext {

/// Paints the area contained within the provided rectangle, using the passed color and texture.
public func drawRect(_ rect: Rect, texture: Texture2D? = nil, color: Color) {
let modelMatrix = self.currentTransform
let transform = rect.toTransform3D
let transform = self.currentTransform * rect.toTransform3D
self.currentDrawContext?.drawQuad(transform: transform, texture: texture, color: color)
}

Expand Down
28 changes: 14 additions & 14 deletions Sources/AdaEngine/UI/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,32 @@ open class UIView {
}

// MARK: Rendering

open func draw(in rect: Rect, with context: GUIRenderContext) {

open func draw(in rect: Rect, with context: GUIRenderContext) { }

// TODO:
/// Internal method for drawing
internal func draw(with context: GUIRenderContext) {
if self.isHidden {
return
}

context.drawRect(rect, color: self.backgroundColor)

for subview in self.zSortedChildren {
subview.draw(with: context)
}
}

internal func draw(with context: GUIRenderContext) {
context.saveContext()

if affineTransform != .identity {
context.multiply(Transform3D(from: affineTransform))
}

// context.translateBy(x: self.frame.minX, y: self.frame.minY)
context.scaleBy(x: 1, y: 1)
context.translateBy(x: self.frame.midX, y: -self.frame.midY)

self.draw(in: self.bounds, with: context)
/// Draw background
context.drawRect(self.bounds, color: self.backgroundColor)

// context.translateBy(x: -self.frame.minX, y: -self.frame.minY)
self.draw(in: self.bounds, with: context)

for subview in self.zSortedChildren {
subview.draw(with: context)
}

context.restoreContext()
}
Expand Down
8 changes: 0 additions & 8 deletions Sources/AdaEngine/UI/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ open class UIWindow: UIView {
super.frameDidChange()
}

override func draw(with context: GUIRenderContext) {
super.draw(with: context)
}

open override func draw(in rect: Rect, with context: GUIRenderContext) {
super.draw(in: rect, with: context)
}

public override func addSubview(_ view: UIView) {
if view is UIWindow {
fatalError("You cannot add window as subview to another window")
Expand Down
2 changes: 1 addition & 1 deletion Sources/AdaEngine/UI/WindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class UIWindowManager {
if window.canDraw {
let context = GUIRenderContext(window: window)

context.beginDraw(in: window.bounds)
context.beginDraw(in: window.bounds.size, scaleFactor: 1)
window.draw(with: context)
context.commitDraw()
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/AdaEngine/Utils/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public extension Color {
static let purple = Color(red: 175 / 255, green: 82 / 255, blue: 222 / 255, alpha: 1)

static let clear = Color(red: 1, green: 1, blue: 1, alpha: 0)

func random() -> Color {
Color(
red: Float.random(in: 0...255) / 255,
green: Float.random(in: 0...255) / 255,
blue: Float.random(in: 0...255) / 255,
alpha: Float.random(in: 0...1)
)
}
}

public extension Color {
Expand Down

0 comments on commit 5b0f852

Please sign in to comment.