1+ @file:Suppress(" FunctionName" )
2+
13package org.polyfrost.polyplus.client.gui
24
35import org.polyfrost.polyui.component.Drawable
46import org.polyfrost.polyui.component.extensions.events
57import org.polyfrost.polyui.component.extensions.named
8+ import org.polyfrost.polyui.component.extensions.namedId
9+ import org.polyfrost.polyui.component.extensions.onClick
610import org.polyfrost.polyui.component.extensions.setPalette
7- import org.polyfrost.polyui.component.impl.Button
11+ import org.polyfrost.polyui.component.extensions.withHoverStates
12+ import org.polyfrost.polyui.component.impl.Block
813import org.polyfrost.polyui.component.impl.Group
14+ import org.polyfrost.polyui.component.impl.Image
915import org.polyfrost.polyui.component.impl.Text
16+ import org.polyfrost.polyui.data.PolyImage
1017import org.polyfrost.polyui.event.Event
1118import org.polyfrost.polyui.event.State
1219import org.polyfrost.polyui.unit.Align
1320import org.polyfrost.polyui.unit.Vec2
21+ import org.polyfrost.polyui.utils.image
1422
1523fun CartControls (count : State <Int >): Drawable {
24+ var cartButton: Drawable ? = null
1625 var checkoutButton: Drawable ? = null
1726
1827 val countListener = countListener@ { count: Int ->
19- var text = checkoutButton?.get(0 )
20- if (text !is Text ) {
21- text = checkoutButton?.get(1 )
28+ cartButton?.let {
29+ updateCartIcon(it, count)
2230 }
2331
24- if (text == null ) {
25- return @countListener false
32+ checkoutButton?. let {
33+ updateCheckoutButtonText(it, count)
2634 }
2735
28- (text as ? Text )?.text = createCheckoutButtonText(count)
2936 false
3037 }
3138
3239 count.listen(countListener)
40+
41+ val currentCartCount = count.value
3342 return Group (
34- Button (
43+ PlusButton (
44+ image = createCartIconPath(currentCartCount).image(),
3545 text = " Cart" ,
3646 radii = floatArrayOf(6f ),
37- padding = Vec2 (42.75f , 5.5f ),
3847 size = Vec2 (146f , 32f )
39- ).named(" CartButton" ),
40- Button (
41- text = createCheckoutButtonText(count.value),
48+ ).named(" CartButton" ).also { cartButton = it },
49+ PlusButton (
50+ image = " /assets/polyplus/ico/shopping-bag.svg" .image(),
51+ text = createCheckoutButtonText(currentCartCount),
4252 radii = floatArrayOf(6f ),
43- padding = Vec2 (75.5f , 5.5f ),
4453 size = Vec2 (306f , 32f )
45- ).setPalette { brand.fg }.named(" CheckoutButton" ).also { checkoutButton = it },
54+ ).onClick {
55+ count.value++
56+ Unit
57+ }.setPalette { brand.fg }.named(" CheckoutButton" ).also { checkoutButton = it },
4658 size = Vec2 (465f , 32f ),
47- alignment = Align (padBetween = Vec2 (13f , 0f ))
59+ alignment = Align (padBetween = Vec2 (13f , 0f ), padEdges = Vec2 . ZERO , wrap = Align . Wrap . NEVER )
4860 ).events {
4961 Event .Lifetime .Removed then {
5062 count.removeListener(countListener)
@@ -53,10 +65,57 @@ fun CartControls(count: State<Int>): Drawable {
5365 }.named(" CartControls" )
5466}
5567
68+ private fun PlusButton (
69+ image : PolyImage ,
70+ text : String ,
71+ radii : FloatArray ,
72+ padding : Vec2 = Vec2 (12f, 6f),
73+ size : Vec2 = Vec2 .ZERO ,
74+ ): Block {
75+ return Block (
76+ Image (image).named(" Icon" ),
77+ Text (text, fontSize = 12f ),
78+ radii = radii,
79+ size = size,
80+ alignment = Align (main = Align .Content .Center , wrap = Align .Wrap .NEVER , pad = padding)
81+ ).withHoverStates().namedId(" PlusButton" )
82+ }
83+
84+ private fun createCartIconPath (count : Int ): String {
85+ if (true ) {
86+ // Just until the rest are done
87+ return " /assets/polyplus/ico/shopping-cart/0.svg"
88+ }
89+
90+ return when {
91+ count <= 0 -> " /assets/polyplus/ico/shopping-cart/0.svg"
92+ count in 1 .. 9 -> " /assets/polyplus/ico/shopping-cart/$count .svg"
93+ else -> " /assets/polyplus/ico/shopping-cart/9+.svg"
94+ }
95+ }
96+
97+ private fun updateCartIcon (button : Drawable , count : Int ) {
98+ var icon = button[0 ]
99+ if (icon !is Image ) {
100+ icon = button[1 ]
101+ }
102+
103+ (icon as ? Image )?.image = createCartIconPath(count).image()
104+ }
105+
56106private fun createCheckoutButtonText (count : Int ): String {
57107 return if (count > 0 ) {
58108 " Checkout $count items"
59109 } else {
60110 " Checkout"
61111 }
62112}
113+
114+ private fun updateCheckoutButtonText (button : Drawable , count : Int ) {
115+ var text = button[0 ]
116+ if (text !is Text ) {
117+ text = button[1 ]
118+ }
119+
120+ (text as ? Text )?.text = createCheckoutButtonText(count)
121+ }
0 commit comments