Skip to content

Commit 273b0b7

Browse files
committed
feat: implement DSL-style builders for Title and Title Times with comprehensive documentation
1 parent 6d04779 commit 273b0b7

File tree

1 file changed

+191
-1
lines changed
  • surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/messages/adventure

1 file changed

+191
-1
lines changed

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/messages/adventure/title-extensions.kt

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,283 @@ import kotlin.time.Duration
88
import kotlin.time.toJavaDuration
99
import java.time.Duration as JavaDuration
1010

11+
/**
12+
* A DSL marker for the Title DSL to prevent scope conflicts in nested DSL blocks.
13+
*/
1114
@DslMarker
1215
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
1316
annotation class TitleDsl
1417

18+
/**
19+
* A DSL marker for the Title Times DSL to prevent scope conflicts in nested DSL blocks.
20+
*/
1521
@DslMarker
1622
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
1723
annotation class TitleTimeDsl
1824

25+
/**
26+
* Creates a [Title] using the DSL-style builder.
27+
*
28+
* @param block The configuration block for building the title.
29+
* @return A configured [Title] instance.
30+
*
31+
* **Example Usage:**
32+
* ```kotlin
33+
* val myTitle = title {
34+
* title { appendText("Welcome!", PRIMARY) }
35+
* subtitle { appendText("Enjoy your stay.", SECONDARY) }
36+
* times {
37+
* fadeIn(20) // 1 second fade-in
38+
* stay(100) // 5 seconds display
39+
* fadeOut(40) // 2 seconds fade-out
40+
* }
41+
* }
42+
* ```
43+
*/
1944
inline fun title(block: @TitleDsl TitleBuilder.() -> Unit): Title {
2045
return TitleBuilder().apply(block).build()
2146
}
2247

48+
/**
49+
* Creates a [Title] using the DSL-style builder. This is an alias for [title].
50+
*
51+
* @param block The configuration block for building the title.
52+
* @return A configured [Title] instance.
53+
* @see title
54+
*/
2355
inline fun Title(block: @TitleDsl TitleBuilder.() -> Unit) = title(block)
2456

57+
/**
58+
* Creates a [Title.Times] instance using the DSL-style builder.
59+
*
60+
* @param block The configuration block for setting title timing.
61+
* @return A configured [Title.Times] instance.
62+
*
63+
* **Example Usage:**
64+
* ```kotlin
65+
* val times = titleTimes {
66+
* fadeIn(20)
67+
* stay(80)
68+
* fadeOut(30)
69+
* }
70+
* ```
71+
*/
2572
inline fun titleTimes(block: @TitleTimeDsl TitleTimesBuilder.() -> Unit): Title.Times {
2673
return TitleTimesBuilder().apply(block).build()
2774
}
2875

76+
/**
77+
* A DSL builder for creating a [Title].
78+
*/
2979
@TitleDsl
3080
class TitleBuilder {
81+
/**
82+
* The main title text of the [Title].
83+
*/
3184
var title: Component = Component.empty()
85+
86+
/**
87+
* The subtitle text of the [Title].
88+
*/
3289
var subtitle: Component = Component.empty()
90+
91+
/**
92+
* The timing settings for the [Title]. Defaults to [Title.DEFAULT_TIMES].
93+
*/
3394
internal var times: Title.Times = Title.DEFAULT_TIMES
3495

3596

97+
/**
98+
* Sets the main title text using a component builder.
99+
*
100+
* @param block The configuration block for creating the title component.
101+
*
102+
* **Example Usage:**
103+
* ```kotlin
104+
* title {
105+
* title { appendText("Game Over", ERROR) }
106+
* }
107+
* ```
108+
*/
36109
inline fun title(block: @TitleDsl SurfComponentBuilder.() -> Unit) {
37110
title = buildText(block)
38111
}
39112

113+
/**
114+
* Sets the subtitle text using a component builder.
115+
*
116+
* @param block The configuration block for creating the subtitle component.
117+
*
118+
* **Example Usage:**
119+
* ```kotlin
120+
* title {
121+
* subtitle { appendText("You have lost the game.", SECONDARY) }
122+
* }
123+
* ```
124+
*/
40125
inline fun subtitle(block: @TitleDsl SurfComponentBuilder.() -> Unit) {
41126
subtitle = buildText(block)
42127
}
43128

129+
/**
130+
* Sets the timing for the title display.
131+
*
132+
* @param block The configuration block for setting fade-in, stay, and fade-out times.
133+
*
134+
* **Example Usage:**
135+
* ```kotlin
136+
* title {
137+
* times {
138+
* fadeIn(20)
139+
* stay(100)
140+
* fadeOut(40)
141+
* }
142+
* }
143+
* ```
144+
*/
44145
fun times(block: @TitleDsl TitleTimesBuilder.() -> Unit) {
45146
times = TitleTimesBuilder().apply(block).build()
46147
}
47148

149+
/**
150+
* Builds and returns the configured [Title] instance.
151+
*
152+
* @return The constructed [Title] object.
153+
*/
48154
@PublishedApi
49155
internal fun build(): Title {
50156
return Title.title(title, subtitle, times)
51157
}
52158
}
53159

54-
160+
/**
161+
* A DSL builder for setting the fade-in, stay, and fade-out times of a [Title].
162+
*/
55163
@TitleTimeDsl
56164
class TitleTimesBuilder {
165+
/**
166+
* The fade-in duration before the title fully appears.
167+
*/
57168
internal var fadeIn: JavaDuration = Ticks.duration(10)
169+
170+
/**
171+
* The duration for which the title remains visible.
172+
*/
58173
internal var stay: JavaDuration = Ticks.duration(70)
174+
175+
/**
176+
* The fade-out duration before the title disappears.
177+
*/
59178
internal var fadeOut: JavaDuration = Ticks.duration(20)
60179

61180

181+
/**
182+
* Sets the fade-in duration in ticks.
183+
*
184+
* @param ticks The duration in ticks.
185+
*
186+
* **Example Usage:**
187+
* ```kotlin
188+
* times {
189+
* fadeIn(30) // 1.5 seconds
190+
* }
191+
* ```
192+
*/
62193
fun fadeIn(ticks: Long) {
63194
fadeIn = Ticks.duration(ticks)
64195
}
65196

197+
/**
198+
* Sets the fade-in duration using a [JavaDuration].
199+
*
200+
* @param duration The duration.
201+
*/
66202
fun fadeIn(duration: JavaDuration) {
67203
fadeIn = duration
68204
}
69205

206+
/**
207+
* Sets the fade-in duration using a [Duration].
208+
*
209+
* @param duration The duration.
210+
*/
70211
fun fadeIn(duration: Duration) {
71212
fadeIn = duration.toJavaDuration()
72213
}
73214

215+
/**
216+
* Sets the stay duration in ticks.
217+
*
218+
* @param ticks The duration in ticks.
219+
*
220+
* **Example Usage:**
221+
* ```kotlin
222+
* times {
223+
* stay(80) // 4 seconds
224+
* }
225+
* ```
226+
*/
74227
fun stay(ticks: Long) {
75228
stay = Ticks.duration(ticks)
76229
}
77230

231+
/**
232+
* Sets the stay duration using a [JavaDuration].
233+
*
234+
* @param duration The duration.
235+
*/
78236
fun stay(duration: JavaDuration) {
79237
stay = duration
80238
}
81239

240+
/**
241+
* Sets the stay duration using a [Duration].
242+
*
243+
* @param duration The duration.
244+
*/
82245
fun stay(duration: Duration) {
83246
stay = duration.toJavaDuration()
84247
}
85248

249+
/**
250+
* Sets the fade-out duration in ticks.
251+
*
252+
* @param ticks The duration in ticks.
253+
*
254+
* **Example Usage:**
255+
* ```kotlin
256+
* times {
257+
* fadeOut(40) // 2 seconds
258+
* }
259+
* ```
260+
*/
86261
fun fadeOut(ticks: Long) {
87262
fadeOut = Ticks.duration(ticks)
88263
}
89264

265+
/**
266+
* Sets the fade-out duration using a [JavaDuration].
267+
*
268+
* @param duration The duration.
269+
*/
90270
fun fadeOut(duration: JavaDuration) {
91271
fadeOut = duration
92272
}
93273

274+
/**
275+
* Sets the fade-out duration using a [Duration].
276+
*
277+
* @param duration The duration.
278+
*/
94279
fun fadeOut(duration: Duration) {
95280
fadeOut = duration.toJavaDuration()
96281
}
97282

283+
/**
284+
* Builds and returns the configured [Title.Times] instance.
285+
*
286+
* @return The constructed [Title.Times] object.
287+
*/
98288
@PublishedApi
99289
internal fun build(): Title.Times {
100290
return Title.Times.times(fadeIn, stay, fadeOut)

0 commit comments

Comments
 (0)