Compare commits
3
Commits
master
...
revert/clay-ui
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b12022b07f | ||
|
|
0d3ae58b40 | ||
|
|
6b68ce3fa1 |
@@ -1,601 +0,0 @@
|
||||
package clay
|
||||
|
||||
import "core:c"
|
||||
|
||||
when ODIN_OS == .Windows {
|
||||
foreign import Clay "windows/clay.lib"
|
||||
} else when ODIN_OS == .Linux {
|
||||
foreign import Clay "linux/clay.a"
|
||||
} else when ODIN_OS == .Darwin {
|
||||
when ODIN_ARCH == .arm64 {
|
||||
foreign import Clay "macos-arm64/clay.a"
|
||||
} else {
|
||||
foreign import Clay "macos/clay.a"
|
||||
}
|
||||
} else when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 {
|
||||
foreign import Clay "wasm/clay.o"
|
||||
}
|
||||
|
||||
String :: struct {
|
||||
isStaticallyAllocated: c.bool,
|
||||
length: c.int32_t,
|
||||
chars: [^]c.char,
|
||||
}
|
||||
|
||||
StringSlice :: struct {
|
||||
length: c.int32_t,
|
||||
chars: [^]c.char,
|
||||
baseChars: [^]c.char,
|
||||
}
|
||||
|
||||
Vector2 :: [2]c.float
|
||||
|
||||
Dimensions :: struct {
|
||||
width: c.float,
|
||||
height: c.float,
|
||||
}
|
||||
|
||||
Arena :: struct {
|
||||
nextAllocation: uintptr,
|
||||
capacity: c.size_t,
|
||||
memory: [^]c.char,
|
||||
}
|
||||
|
||||
BoundingBox :: struct {
|
||||
x: c.float,
|
||||
y: c.float,
|
||||
width: c.float,
|
||||
height: c.float,
|
||||
}
|
||||
|
||||
Color :: [4]c.float
|
||||
|
||||
CornerRadius :: struct {
|
||||
topLeft: c.float,
|
||||
topRight: c.float,
|
||||
bottomLeft: c.float,
|
||||
bottomRight: c.float,
|
||||
}
|
||||
|
||||
ElementId :: struct {
|
||||
id: u32,
|
||||
offset: u32,
|
||||
baseId: u32,
|
||||
stringId: String,
|
||||
}
|
||||
|
||||
ElementIdArray :: struct {
|
||||
capacity: i32,
|
||||
length: i32,
|
||||
internalArray: [^]ElementId,
|
||||
}
|
||||
|
||||
when ODIN_OS == .Windows {
|
||||
EnumBackingType :: u32
|
||||
} else {
|
||||
EnumBackingType :: u8
|
||||
}
|
||||
|
||||
RenderCommandType :: enum EnumBackingType {
|
||||
None,
|
||||
Rectangle,
|
||||
Border,
|
||||
Text,
|
||||
Image,
|
||||
ScissorStart,
|
||||
ScissorEnd,
|
||||
OverlayColorStart,
|
||||
OverlayColorEnd,
|
||||
Custom,
|
||||
}
|
||||
|
||||
RectangleElementConfig :: struct {
|
||||
color: Color,
|
||||
}
|
||||
|
||||
TextWrapMode :: enum EnumBackingType {
|
||||
Words,
|
||||
Newlines,
|
||||
None,
|
||||
}
|
||||
|
||||
TextAlignment :: enum EnumBackingType {
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
}
|
||||
|
||||
TextElementConfig :: struct {
|
||||
userData: rawptr,
|
||||
textColor: Color,
|
||||
fontId: u16,
|
||||
fontSize: u16,
|
||||
letterSpacing: u16,
|
||||
lineHeight: u16,
|
||||
wrapMode: TextWrapMode,
|
||||
textAlignment: TextAlignment,
|
||||
}
|
||||
|
||||
AspectRatioElementConfig :: struct {
|
||||
aspectRatio: f32,
|
||||
}
|
||||
|
||||
ImageElementConfig :: struct {
|
||||
imageData: rawptr,
|
||||
}
|
||||
|
||||
CustomElementConfig :: struct {
|
||||
customData: rawptr,
|
||||
}
|
||||
|
||||
BorderWidth :: struct {
|
||||
left: u16,
|
||||
right: u16,
|
||||
top: u16,
|
||||
bottom: u16,
|
||||
betweenChildren: u16,
|
||||
}
|
||||
|
||||
BorderElementConfig :: struct {
|
||||
color: Color,
|
||||
width: BorderWidth,
|
||||
}
|
||||
|
||||
TransitionData :: struct {
|
||||
boundingBox: BoundingBox,
|
||||
backgroundColor: Color,
|
||||
overlayColor: Color,
|
||||
borderColor: Color,
|
||||
borderWidth: BorderWidth,
|
||||
}
|
||||
|
||||
TransitionState :: enum c.int {
|
||||
Idle,
|
||||
Entering,
|
||||
Transitioning,
|
||||
Exiting,
|
||||
}
|
||||
|
||||
TransitionProperty :: enum c.int {
|
||||
X,
|
||||
Y,
|
||||
Width,
|
||||
Height,
|
||||
BackgroundColor,
|
||||
OverlayColor,
|
||||
CornerRadius,
|
||||
BorderColor,
|
||||
BorderWidth,
|
||||
}
|
||||
|
||||
TransitionPropertyFlags :: bit_set[TransitionProperty;c.int]
|
||||
TransitionPropertyPosition :: TransitionPropertyFlags{.X, .Y}
|
||||
TransitionPropertyDimensions :: TransitionPropertyFlags{.Width, .Height}
|
||||
TransitionPropertyBoundingBox :: TransitionPropertyPosition + TransitionPropertyDimensions
|
||||
TransitionPropertyBorder :: TransitionPropertyFlags{.BorderColor, .BorderWidth}
|
||||
|
||||
TransitionCallbackArguments :: struct {
|
||||
transitionState: TransitionState,
|
||||
initial: TransitionData,
|
||||
current: ^TransitionData,
|
||||
target: TransitionData,
|
||||
elapsedTime: f32,
|
||||
duration: f32,
|
||||
properties: TransitionPropertyFlags,
|
||||
}
|
||||
|
||||
TransitionEnterTriggerType :: enum EnumBackingType {
|
||||
SkipOnFirstParentFrame,
|
||||
TriggerOnFirstParentFrame,
|
||||
}
|
||||
|
||||
TransitionExitTriggerType :: enum EnumBackingType {
|
||||
SkipWhenParentExits,
|
||||
TriggerWhenParentExits,
|
||||
}
|
||||
|
||||
TransitionInteractionHandlingType :: enum EnumBackingType {
|
||||
DisableInteractionsWhileTransitioningPosition,
|
||||
AllowInteractionsWhileTransitioningPosition,
|
||||
}
|
||||
|
||||
ExitTransitionSiblingOrdering :: enum EnumBackingType {
|
||||
UnderneathSiblings,
|
||||
NaturalOrder,
|
||||
AboveSiblings,
|
||||
}
|
||||
|
||||
TransitionElementConfig :: struct {
|
||||
handler: proc "c" (args: TransitionCallbackArguments) -> bool,
|
||||
duration: f32,
|
||||
properties: TransitionPropertyFlags,
|
||||
interactionHandling: TransitionInteractionHandlingType,
|
||||
enter: struct {
|
||||
setInitialState: proc "c" (initialState: TransitionData, properties: TransitionPropertyFlags) -> TransitionData,
|
||||
trigger: TransitionEnterTriggerType,
|
||||
},
|
||||
exit: struct {
|
||||
setFinalState: proc "c" (finalState: TransitionData, properties: TransitionPropertyFlags) -> TransitionData,
|
||||
trigger: TransitionExitTriggerType,
|
||||
siblingOrdering: ExitTransitionSiblingOrdering,
|
||||
},
|
||||
}
|
||||
|
||||
ClipElementConfig :: struct {
|
||||
horizontal: bool, // clip overflowing elements on the "X" axis
|
||||
vertical: bool, // clip overflowing elements on the "Y" axis
|
||||
childOffset: Vector2, // offsets the [X,Y] positions of all child elements, primarily for scrolling containers
|
||||
}
|
||||
|
||||
FloatingAttachPointType :: enum EnumBackingType {
|
||||
LeftTop,
|
||||
LeftCenter,
|
||||
LeftBottom,
|
||||
CenterTop,
|
||||
CenterCenter,
|
||||
CenterBottom,
|
||||
RightTop,
|
||||
RightCenter,
|
||||
RightBottom,
|
||||
}
|
||||
|
||||
FloatingAttachPoints :: struct {
|
||||
element: FloatingAttachPointType,
|
||||
parent: FloatingAttachPointType,
|
||||
}
|
||||
|
||||
PointerCaptureMode :: enum EnumBackingType {
|
||||
Capture,
|
||||
Passthrough,
|
||||
}
|
||||
|
||||
FloatingAttachToElement :: enum EnumBackingType {
|
||||
None,
|
||||
Parent,
|
||||
ElementWithId,
|
||||
Root,
|
||||
}
|
||||
|
||||
FloatingClipToElement :: enum EnumBackingType {
|
||||
None,
|
||||
AttachedParent,
|
||||
}
|
||||
|
||||
FloatingElementConfig :: struct {
|
||||
offset: Vector2,
|
||||
expand: Dimensions,
|
||||
parentId: u32,
|
||||
zIndex: i16,
|
||||
attachment: FloatingAttachPoints,
|
||||
pointerCaptureMode: PointerCaptureMode,
|
||||
attachTo: FloatingAttachToElement,
|
||||
clipTo: FloatingClipToElement,
|
||||
}
|
||||
|
||||
TextRenderData :: struct {
|
||||
stringContents: StringSlice,
|
||||
textColor: Color,
|
||||
fontId: u16,
|
||||
fontSize: u16,
|
||||
letterSpacing: u16,
|
||||
lineHeight: u16,
|
||||
}
|
||||
|
||||
RectangleRenderData :: struct {
|
||||
backgroundColor: Color,
|
||||
cornerRadius: CornerRadius,
|
||||
}
|
||||
|
||||
ImageRenderData :: struct {
|
||||
backgroundColor: Color,
|
||||
cornerRadius: CornerRadius,
|
||||
imageData: rawptr,
|
||||
}
|
||||
|
||||
CustomRenderData :: struct {
|
||||
backgroundColor: Color,
|
||||
cornerRadius: CornerRadius,
|
||||
customData: rawptr,
|
||||
}
|
||||
|
||||
ClipRenderData :: struct {
|
||||
horizontal: bool,
|
||||
vertical: bool,
|
||||
}
|
||||
|
||||
OverlayColorRenderData :: struct {
|
||||
color: Color,
|
||||
}
|
||||
|
||||
BorderRenderData :: struct {
|
||||
color: Color,
|
||||
cornerRadius: CornerRadius,
|
||||
width: BorderWidth,
|
||||
}
|
||||
|
||||
RenderCommandData :: struct #raw_union {
|
||||
rectangle: RectangleRenderData,
|
||||
text: TextRenderData,
|
||||
image: ImageRenderData,
|
||||
custom: CustomRenderData,
|
||||
border: BorderRenderData,
|
||||
clip: ClipRenderData,
|
||||
overlayColor: OverlayColorRenderData,
|
||||
}
|
||||
|
||||
RenderCommand :: struct {
|
||||
boundingBox: BoundingBox,
|
||||
renderData: RenderCommandData,
|
||||
userData: rawptr,
|
||||
id: u32,
|
||||
zIndex: i16,
|
||||
commandType: RenderCommandType,
|
||||
}
|
||||
|
||||
ScrollContainerData :: struct {
|
||||
// Note: This is a pointer to the real internal scroll position, mutating it may cause a change in final layout.
|
||||
// Intended for use with external functionality that modifies scroll position, such as scroll bars or auto scrolling.
|
||||
scrollPosition: ^Vector2,
|
||||
scrollContainerDimensions: Dimensions,
|
||||
contentDimensions: Dimensions,
|
||||
config: ClipElementConfig,
|
||||
// Indicates whether an actual scroll container matched the provided ID or if the default struct was returned.
|
||||
found: bool,
|
||||
}
|
||||
|
||||
ElementData :: struct {
|
||||
boundingBox: BoundingBox,
|
||||
found: bool,
|
||||
}
|
||||
|
||||
PointerDataInteractionState :: enum EnumBackingType {
|
||||
PressedThisFrame,
|
||||
Pressed,
|
||||
ReleasedThisFrame,
|
||||
Released,
|
||||
}
|
||||
|
||||
PointerData :: struct {
|
||||
position: Vector2,
|
||||
state: PointerDataInteractionState,
|
||||
}
|
||||
|
||||
SizingType :: enum EnumBackingType {
|
||||
Fit,
|
||||
Grow,
|
||||
Percent,
|
||||
Fixed,
|
||||
}
|
||||
|
||||
SizingConstraintsMinMax :: struct {
|
||||
min: c.float,
|
||||
max: c.float,
|
||||
}
|
||||
|
||||
SizingConstraints :: struct #raw_union {
|
||||
sizeMinMax: SizingConstraintsMinMax,
|
||||
sizePercent: c.float,
|
||||
}
|
||||
|
||||
SizingAxis :: struct {
|
||||
// Note: `min` is used for CLAY_SIZING_PERCENT, slightly different to clay.h due to lack of C anonymous unions
|
||||
constraints: SizingConstraints,
|
||||
type: SizingType,
|
||||
}
|
||||
|
||||
Sizing :: struct {
|
||||
width: SizingAxis,
|
||||
height: SizingAxis,
|
||||
}
|
||||
|
||||
Padding :: struct {
|
||||
left: u16,
|
||||
right: u16,
|
||||
top: u16,
|
||||
bottom: u16,
|
||||
}
|
||||
|
||||
LayoutDirection :: enum EnumBackingType {
|
||||
LeftToRight,
|
||||
TopToBottom,
|
||||
}
|
||||
|
||||
LayoutAlignmentX :: enum EnumBackingType {
|
||||
Left,
|
||||
Right,
|
||||
Center,
|
||||
}
|
||||
|
||||
LayoutAlignmentY :: enum EnumBackingType {
|
||||
Top,
|
||||
Bottom,
|
||||
Center,
|
||||
}
|
||||
|
||||
ChildAlignment :: struct {
|
||||
x: LayoutAlignmentX,
|
||||
y: LayoutAlignmentY,
|
||||
}
|
||||
|
||||
LayoutConfig :: struct {
|
||||
sizing: Sizing,
|
||||
padding: Padding,
|
||||
childGap: u16,
|
||||
childAlignment: ChildAlignment,
|
||||
layoutDirection: LayoutDirection,
|
||||
}
|
||||
|
||||
ClayArray :: struct($type: typeid) {
|
||||
capacity: i32,
|
||||
length: i32,
|
||||
internalArray: [^]type,
|
||||
}
|
||||
|
||||
ElementDeclaration :: struct {
|
||||
layout: LayoutConfig,
|
||||
backgroundColor: Color,
|
||||
overlayColor: Color,
|
||||
cornerRadius: CornerRadius,
|
||||
aspectRatio: AspectRatioElementConfig,
|
||||
image: ImageElementConfig,
|
||||
floating: FloatingElementConfig,
|
||||
custom: CustomElementConfig,
|
||||
clip: ClipElementConfig,
|
||||
border: BorderElementConfig,
|
||||
transition: TransitionElementConfig,
|
||||
userData: rawptr,
|
||||
}
|
||||
|
||||
ErrorType :: enum EnumBackingType {
|
||||
TextMeasurementFunctionNotProvided,
|
||||
ArenaCapacityExceeded,
|
||||
ElementsCapacityExceeded,
|
||||
TextMeasurementCapacityExceeded,
|
||||
DuplicateId,
|
||||
FloatingContainerParentNotFound,
|
||||
PercentageOver1,
|
||||
InternalError,
|
||||
UnbalancedOpenClose,
|
||||
}
|
||||
|
||||
ErrorData :: struct {
|
||||
errorType: ErrorType,
|
||||
errorText: String,
|
||||
userData: rawptr,
|
||||
}
|
||||
|
||||
ErrorHandler :: struct {
|
||||
handler: proc "c" (errorData: ErrorData),
|
||||
userData: rawptr,
|
||||
}
|
||||
|
||||
Context :: struct {} // opaque structure, only use as a pointer
|
||||
|
||||
@(link_prefix = "Clay_", default_calling_convention = "c")
|
||||
foreign Clay {
|
||||
_OpenElement :: proc() ---
|
||||
_OpenElementWithId :: proc(id: ElementId) ---
|
||||
_CloseElement :: proc() ---
|
||||
MinMemorySize :: proc() -> u32 ---
|
||||
CreateArenaWithCapacityAndMemory :: proc(capacity: c.size_t, offset: [^]u8) -> Arena ---
|
||||
SetPointerState :: proc(position: Vector2, pointerDown: bool) ---
|
||||
GetPointerState :: proc() -> PointerData ---
|
||||
Initialize :: proc(arena: Arena, layoutDimensions: Dimensions, errorHandler: ErrorHandler) -> ^Context ---
|
||||
GetCurrentContext :: proc() -> ^Context ---
|
||||
SetCurrentContext :: proc(ctx: ^Context) ---
|
||||
UpdateScrollContainers :: proc(enableDragScrolling: bool, scrollDelta: Vector2, deltaTime: c.float) ---
|
||||
SetLayoutDimensions :: proc(dimensions: Dimensions) ---
|
||||
BeginLayout :: proc() ---
|
||||
EndLayout :: proc(deltaTime: c.float) -> ClayArray(RenderCommand) ---
|
||||
GetOpenElementId :: proc() -> u32 ---
|
||||
GetElementId :: proc(id: String) -> ElementId ---
|
||||
GetElementIdWithIndex :: proc(id: String, index: u32) -> ElementId ---
|
||||
GetElementData :: proc(id: ElementId) -> ElementData ---
|
||||
Hovered :: proc() -> bool ---
|
||||
OnHover :: proc(onHoverFunction: proc "c" (id: ElementId, pointerData: PointerData, userData: rawptr), userData: rawptr) ---
|
||||
PointerOver :: proc(id: ElementId) -> bool ---
|
||||
GetPointerOverIds :: proc() -> ElementIdArray ---
|
||||
GetScrollOffset :: proc() -> Vector2 ---
|
||||
GetScrollContainerData :: proc(id: ElementId) -> ScrollContainerData ---
|
||||
SetMeasureTextFunction :: proc(measureTextFunction: proc "c" (text: StringSlice, config: ^TextElementConfig, userData: rawptr) -> Dimensions, userData: rawptr) ---
|
||||
SetQueryScrollOffsetFunction :: proc(queryScrollOffsetFunction: proc "c" (elementId: u32, userData: rawptr) -> Vector2, userData: rawptr) ---
|
||||
RenderCommandArray_Get :: proc(array: ^ClayArray(RenderCommand), index: i32) -> ^RenderCommand ---
|
||||
SetDebugModeEnabled :: proc(enabled: bool) ---
|
||||
IsDebugModeEnabled :: proc() -> bool ---
|
||||
SetCullingEnabled :: proc(enabled: bool) ---
|
||||
GetMaxElementCount :: proc() -> i32 ---
|
||||
SetMaxElementCount :: proc(maxElementCount: i32) ---
|
||||
GetMaxMeasureTextCacheWordCount :: proc() -> i32 ---
|
||||
SetMaxMeasureTextCacheWordCount :: proc(maxMeasureTextCacheWordCount: i32) ---
|
||||
ResetMeasureTextCache :: proc() ---
|
||||
EaseOut :: proc(arguments: TransitionCallbackArguments) -> bool ---
|
||||
}
|
||||
|
||||
@(link_prefix = "Clay_", default_calling_convention = "c", private)
|
||||
foreign Clay {
|
||||
_ConfigureOpenElement :: proc(config: ElementDeclaration) ---
|
||||
_HashString :: proc(key: String, seed: u32) -> ElementId ---
|
||||
_HashStringWithOffset :: proc(key: String, index: u32, seed: u32) -> ElementId ---
|
||||
_OpenTextElement :: proc(text: String, textConfig: TextElementConfig) ---
|
||||
}
|
||||
|
||||
ConfigureOpenElement :: proc(config: ElementDeclaration) -> bool {
|
||||
_ConfigureOpenElement(config)
|
||||
return true
|
||||
}
|
||||
|
||||
@(deferred_none = _CloseElement)
|
||||
UI_WithId :: proc(id: ElementId) -> proc(config: ElementDeclaration) -> bool {
|
||||
_OpenElementWithId(id)
|
||||
return ConfigureOpenElement
|
||||
}
|
||||
|
||||
@(deferred_none = _CloseElement)
|
||||
UI_AutoId :: proc() -> proc(config: ElementDeclaration) -> bool {
|
||||
_OpenElement()
|
||||
return ConfigureOpenElement
|
||||
}
|
||||
|
||||
UI :: proc {
|
||||
UI_WithId,
|
||||
UI_AutoId,
|
||||
}
|
||||
|
||||
Text :: proc {
|
||||
TextStatic,
|
||||
TextDynamic,
|
||||
}
|
||||
|
||||
TextStatic :: proc($text: string, config: TextElementConfig) {
|
||||
wrapped := MakeString(text)
|
||||
wrapped.isStaticallyAllocated = true
|
||||
_OpenTextElement(wrapped, config)
|
||||
}
|
||||
|
||||
TextDynamic :: proc(text: string, config: TextElementConfig) {
|
||||
_OpenTextElement(MakeString(text), config)
|
||||
}
|
||||
|
||||
PaddingAll :: proc(allPadding: u16) -> Padding {
|
||||
return {left = allPadding, right = allPadding, top = allPadding, bottom = allPadding}
|
||||
}
|
||||
|
||||
BorderOutside :: proc(width: u16) -> BorderWidth {
|
||||
return {width, width, width, width, 0}
|
||||
}
|
||||
|
||||
BorderAll :: proc(width: u16) -> BorderWidth {
|
||||
return {width, width, width, width, width}
|
||||
}
|
||||
|
||||
CornerRadiusAll :: proc(radius: f32) -> CornerRadius {
|
||||
return CornerRadius{radius, radius, radius, radius}
|
||||
}
|
||||
|
||||
SizingFit :: proc(sizeMinMax: SizingConstraintsMinMax = {}) -> SizingAxis {
|
||||
return SizingAxis{type = SizingType.Fit, constraints = {sizeMinMax = sizeMinMax}}
|
||||
}
|
||||
|
||||
SizingGrow :: proc(sizeMinMax: SizingConstraintsMinMax = {}) -> SizingAxis {
|
||||
return SizingAxis{type = SizingType.Grow, constraints = {sizeMinMax = sizeMinMax}}
|
||||
}
|
||||
|
||||
SizingFixed :: proc(size: c.float) -> SizingAxis {
|
||||
return SizingAxis{type = SizingType.Fixed, constraints = {sizeMinMax = {size, size}}}
|
||||
}
|
||||
|
||||
SizingPercent :: proc(sizePercent: c.float) -> SizingAxis {
|
||||
return SizingAxis{type = SizingType.Percent, constraints = {sizePercent = sizePercent}}
|
||||
}
|
||||
|
||||
MakeString :: proc(label: string) -> String {
|
||||
return String{chars = raw_data(label), length = cast(c.int)len(label)}
|
||||
}
|
||||
|
||||
ID :: proc(label: string, index: u32 = 0) -> ElementId {
|
||||
return _HashString(MakeString(label), index)
|
||||
}
|
||||
|
||||
ID_LOCAL :: proc(label: string, index: u32 = 0) -> ElementId {
|
||||
return _HashStringWithOffset(MakeString(label), index, GetOpenElementId())
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+2
-5
@@ -7,26 +7,23 @@ run :: proc() {
|
||||
defer rl.CloseWindow()
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
init_ui()
|
||||
defer destroy_ui()
|
||||
|
||||
world := init_world()
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
dt := rl.GetFrameTime()
|
||||
|
||||
ui_prepare_frame(&world, dt)
|
||||
update_world(&world, dt)
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.Color{34, 139, 34, 255})
|
||||
|
||||
render_world(&world)
|
||||
render_ui()
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
delete(world.path)
|
||||
delete(world.towers)
|
||||
delete(world.events)
|
||||
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -15,9 +15,6 @@ Command :: struct {
|
||||
}
|
||||
|
||||
gather_commands :: proc(world: ^World) -> Command {
|
||||
overlay_cmd := poll_overlay_command()
|
||||
if overlay_cmd.kind != .None do return overlay_cmd
|
||||
|
||||
if world.phase == .Build && rl.IsKeyPressed(.N) {
|
||||
return Command{kind = .Start_Wave}
|
||||
}
|
||||
@@ -28,6 +25,12 @@ gather_commands :: proc(world: ^World) -> Command {
|
||||
return Command{kind = .Place_Tower, gx = gx, gy = gy}
|
||||
}
|
||||
|
||||
if rl.IsMouseButtonPressed(.LEFT) {
|
||||
mouse := rl.GetMousePosition()
|
||||
gx, gy := screen_to_grid(mouse.x, mouse.y)
|
||||
return Command{kind = .Place_Tower, gx = gx, gy = gy}
|
||||
}
|
||||
|
||||
return Command{kind = .None}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package game
|
||||
|
||||
import clay "../clay-odin"
|
||||
|
||||
MAP_W :: 30 // map width in tiles
|
||||
MAP_H :: 20 // map height in tiles
|
||||
TILE_SIZE :: 32 // size of tiles in pixels
|
||||
@@ -19,9 +17,3 @@ MAX_PROJECTILES :: 64
|
||||
PROJECTILE_SPEED :: 280 // pixels per second
|
||||
PROJECTILE_HIT_RADIUS :: 10
|
||||
|
||||
UI_BAR_COLOR :: clay.Color{20, 40, 20, 200}
|
||||
UI_TEXT_COLOR :: clay.Color{240, 240, 230, 255}
|
||||
UI_GOLD_COLOR :: clay.Color{255, 215, 80, 255}
|
||||
UI_BUTTON_COLOR :: clay.Color{60, 120, 60, 255}
|
||||
UI_BUTTON_HOVER :: clay.Color{80, 150, 80, 255}
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package game
|
||||
|
||||
import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
render_hud :: proc(world: ^World) {
|
||||
rl.DrawText(fmt.ctprintf("Gold: %d", world.gold), 10, 10, 22, rl.BLACK) // Gold HUD
|
||||
rl.DrawText(fmt.ctprintf("Base HP: %d", world.base_health), 10, 32, 22, rl.BLACK) // HP HUD
|
||||
rl.DrawText(fmt.ctprintf("Enemies: %d", active_enemy_count(world)), 10, 54, 22, rl.BLACK)
|
||||
rl.DrawText(fmt.ctprintf("Wave: %d / %d", world.wave, MAX_WAVES), 10, 76, 22, rl.BLACK)
|
||||
|
||||
switch world.phase {
|
||||
case .Build:
|
||||
rl.DrawText("Build - click towers, N = start wave", 10, 98, 22, rl.BLACK)
|
||||
case .Combat:
|
||||
rl.DrawText("Combat!", 10, 98, 22, rl.MAROON)
|
||||
case .Game_Over:
|
||||
rl.DrawText("GAME_OVER", 260, 280, 40, rl.RED)
|
||||
case .Victory:
|
||||
rl.DrawText("Victory!", 260, 280, 40, rl.GREEN)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
package game
|
||||
|
||||
import clay "../clay-odin"
|
||||
import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
ui_hud_scratch: struct {
|
||||
gold: [64]u8,
|
||||
hp: [64]u8,
|
||||
wave: [64]u8,
|
||||
}
|
||||
|
||||
build_overlay :: proc(world: ^World) {
|
||||
if clay.UI(clay.ID("HudBar"))(
|
||||
{
|
||||
layout = {
|
||||
layoutDirection = .LeftToRight,
|
||||
sizing = {width = clay.SizingGrow({}), height = clay.SizingFixed(28)},
|
||||
padding = clay.PaddingAll(6),
|
||||
childGap = 8,
|
||||
childAlignment = {y = .Center},
|
||||
},
|
||||
backgroundColor = UI_BAR_COLOR,
|
||||
},
|
||||
) {
|
||||
gold_text := fmt.bprintf(ui_hud_scratch.gold[:], "Gold: %d", world.gold)
|
||||
clay.Text(
|
||||
gold_text,
|
||||
clay.TextElementConfig{textColor = UI_GOLD_COLOR, fontId = UI_FONT_ID, fontSize = 18},
|
||||
)
|
||||
|
||||
hp_text := fmt.bprintf(ui_hud_scratch.hp[:], "Base HP: %d", world.base_health)
|
||||
clay.Text(
|
||||
hp_text,
|
||||
clay.TextElementConfig{textColor = UI_TEXT_COLOR, fontId = UI_FONT_ID, fontSize = 18},
|
||||
)
|
||||
|
||||
wave_text := fmt.bprintf(ui_hud_scratch.wave[:], "Wave: %d / %d", world.wave, MAX_WAVES)
|
||||
clay.Text(
|
||||
wave_text,
|
||||
clay.TextElementConfig{textColor = UI_TEXT_COLOR, fontId = UI_FONT_ID, fontSize = 18},
|
||||
)
|
||||
|
||||
if world.phase == .Build {
|
||||
if clay.UI(clay.ID("StartWaveBtn"))(
|
||||
{
|
||||
layout = {
|
||||
sizing = {width = clay.SizingFixed(110), height = clay.SizingFixed(22)},
|
||||
padding = clay.PaddingAll(4),
|
||||
childAlignment = {x = .Center, y = .Center},
|
||||
},
|
||||
backgroundColor = clay.Hovered() ? UI_BUTTON_HOVER : UI_BUTTON_COLOR,
|
||||
},
|
||||
) {
|
||||
clay.Text(
|
||||
"Start Wave",
|
||||
clay.TextElementConfig {
|
||||
textColor = UI_TEXT_COLOR,
|
||||
fontId = UI_FONT_ID,
|
||||
fontSize = 16,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
render_overlay :: proc(world: ^World) {
|
||||
switch world.phase {
|
||||
case .Build:
|
||||
case .Combat:
|
||||
rl.DrawText("Combat!", 10, 98, 22, rl.MAROON)
|
||||
case .Game_Over:
|
||||
rl.DrawText("GAME_OVER", 260, 280, 40, rl.RED)
|
||||
case .Victory:
|
||||
rl.DrawText("Victory!", 260, 280, 40, rl.GREEN)
|
||||
}
|
||||
}
|
||||
|
||||
poll_overlay_command :: proc() -> Command {
|
||||
if clay.PointerOver(clay.ID("StartWaveBtn")) {
|
||||
ptr := clay.GetPointerState()
|
||||
if ptr.state == .PressedThisFrame {
|
||||
return Command{kind = .Start_Wave}
|
||||
}
|
||||
}
|
||||
return Command{kind = .None}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,6 +5,6 @@ render_world :: proc(world: ^World) {
|
||||
render_enemies(world)
|
||||
render_towers(world)
|
||||
render_projectiles(world)
|
||||
render_overlay(world)
|
||||
render_hud(world)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package game
|
||||
|
||||
import clay "../clay-odin"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
ui_memory: [^]u8
|
||||
ui_arena: clay.Arena
|
||||
ui_render_commands: clay.ClayArray(clay.RenderCommand)
|
||||
|
||||
ui_error_handler :: proc "c" (data: clay.ErrorData) {
|
||||
_ = data
|
||||
}
|
||||
|
||||
init_ui :: proc() {
|
||||
ui_register_font()
|
||||
|
||||
min_size := clay.MinMemorySize()
|
||||
ui_memory = make([^]u8, min_size)
|
||||
ui_arena = clay.CreateArenaWithCapacityAndMemory(uint(min_size), ui_memory)
|
||||
clay.Initialize(ui_arena, {f32(SCREEN_W), f32(SCREEN_H)}, {handler = ui_error_handler})
|
||||
clay.SetMeasureTextFunction(measure_text, nil)
|
||||
|
||||
}
|
||||
|
||||
destroy_ui :: proc() {
|
||||
free(ui_memory)
|
||||
}
|
||||
|
||||
ui_prepare_frame :: proc(world: ^World, dt: f32) {
|
||||
mouse := rl.GetMousePosition()
|
||||
clay.SetLayoutDimensions({f32(SCREEN_W), f32(SCREEN_H)})
|
||||
clay.SetPointerState({mouse.x, mouse.y}, rl.IsMouseButtonDown(.LEFT))
|
||||
clay.UpdateScrollContainers(false, {}, dt)
|
||||
|
||||
clay.BeginLayout()
|
||||
build_overlay(world)
|
||||
ui_render_commands = clay.EndLayout(dt)
|
||||
}
|
||||
|
||||
render_ui :: proc() {
|
||||
clay_raylib_render(&ui_render_commands)
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package game
|
||||
|
||||
import clay "../clay-odin"
|
||||
import "core:math"
|
||||
import "core:strings"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
UI_FONT_ID :: 0
|
||||
|
||||
ui_font: rl.Font
|
||||
|
||||
ui_register_font :: proc() {
|
||||
ui_font = rl.GetFontDefault()
|
||||
}
|
||||
|
||||
clay_color_to_rl :: proc(c: clay.Color) -> rl.Color {
|
||||
return {u8(c.r), u8(c.g), u8(c.b), u8(c.a)}
|
||||
}
|
||||
|
||||
measure_text :: proc "c" (
|
||||
text: clay.StringSlice,
|
||||
config: ^clay.TextElementConfig,
|
||||
_: rawptr,
|
||||
) -> clay.Dimensions {
|
||||
return {width = f32(text.length) * f32(config.fontSize) * 0.55, height = f32(config.fontSize)}
|
||||
}
|
||||
|
||||
clay_raylib_render :: proc(commands: ^clay.ClayArray(clay.RenderCommand)) {
|
||||
for i in 0 ..< commands.length {
|
||||
cmd := clay.RenderCommandArray_Get(commands, i)
|
||||
bounds := cmd.boundingBox
|
||||
|
||||
#partial switch cmd.commandType {
|
||||
case .None:
|
||||
case .Text:
|
||||
cfg := cmd.renderData.text
|
||||
text := string(cfg.stringContents.chars[:cfg.stringContents.length])
|
||||
cstr := strings.clone_to_cstring(text, context.temp_allocator)
|
||||
|
||||
rl.DrawTextEx(
|
||||
ui_font,
|
||||
cstr,
|
||||
{bounds.x, bounds.y},
|
||||
f32(cfg.fontSize),
|
||||
f32(cfg.letterSpacing),
|
||||
clay_color_to_rl(cfg.textColor),
|
||||
)
|
||||
case .Rectangle:
|
||||
cfg := cmd.renderData.rectangle
|
||||
rl.DrawRectangle(
|
||||
i32(math.round(bounds.x)),
|
||||
i32(math.round(bounds.y)),
|
||||
i32(math.round(bounds.width)),
|
||||
i32(math.round(bounds.height)),
|
||||
clay_color_to_rl(cfg.backgroundColor),
|
||||
)
|
||||
case .ScissorStart:
|
||||
rl.BeginScissorMode(
|
||||
i32(math.round(bounds.x)),
|
||||
i32(math.round(bounds.y)),
|
||||
i32(math.round(bounds.width)),
|
||||
i32(math.round(bounds.height)),
|
||||
)
|
||||
case .ScissorEnd:
|
||||
rl.EndScissorMode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user