Skip to content

Commit

Permalink
Introduce GUIFixedSizeElement protocol to workaround some annoying co…
Browse files Browse the repository at this point in the history
…nstraint solving limitations (fixes #189)
  • Loading branch information
stackotter committed Jan 14, 2024
1 parent ec79c1d commit e3f224e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/Core/Renderer/GUI/GUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ struct GUI {
group.add(GUISprite.selectedHotbarSlot, .bottom(0), .left(20 * selectedSlot))

for (i, slot) in slots.enumerated() {
group.add(GUIInventorySlot(slot: slot), .bottom(4), .left(20 * i + 4))
group.add(GUIInventorySlot(slot: slot), .bottom(2), .left(20 * i + 4))
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/Renderer/GUI/GUIElement/GUIGroupElement.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FirebladeMath

struct GUIGroupElement: GUIElement {
struct GUIGroupElement: GUIFixedSizeElement {
var size: Vec2i
var children: [(GUIElement, Constraints)]

Expand All @@ -27,7 +27,7 @@ struct GUIGroupElement: GUIElement {
var elementMeshes = try element.meshes(context: context)

let elementSize: Vec2i
if let group = element as? GUIGroupElement {
if let group = element as? GUIFixedSizeElement {
elementSize = group.size
} else {
elementSize = elementMeshes.size()
Expand Down
4 changes: 3 additions & 1 deletion Sources/Core/Renderer/GUI/GUIElement/GUIInventoryItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import Foundation
import FirebladeMath
import DeltaCore

struct GUIInventoryItem: GUIElement {
struct GUIInventoryItem: GUIElement, GUIFixedSizeElement {
var itemId: Int

let size = Vec2i(16, 16)

func meshes(context: GUIContext) throws -> [GUIElementMesh] {
guard let model = context.itemModelPalette.model(for: itemId) else {
throw GUIRendererError.invalidItemId(itemId)
Expand Down
4 changes: 3 additions & 1 deletion Sources/Core/Renderer/GUI/GUIElement/GUIInventorySlot.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import FirebladeMath
import DeltaCore

struct GUIInventorySlot: GUIElement {
struct GUIInventorySlot: GUIElement, GUIFixedSizeElement {
var slot: Slot

let size = Vec2i(17, 18)

func meshes(context: GUIContext) throws -> [GUIElementMesh] {
guard let stack = slot.stack else {
return []
Expand Down
5 changes: 5 additions & 0 deletions Sources/Core/Renderer/GUI/GUIFixedSizeElement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import FirebladeMath

protocol GUIFixedSizeElement: GUIElement {
var size: Vec2i { get }
}

0 comments on commit e3f224e

Please sign in to comment.