Skip to content

Commit 8dd0507

Browse files
committed
migrate to Scala 3 new indent syntax
1 parent b3fbdfe commit 8dd0507

File tree

8 files changed

+66
-132
lines changed

8 files changed

+66
-132
lines changed

src/main/scala/introprog/BlockGame.scala

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class BlockGame(
2020
var framesPerSecond: Int = 50,
2121
val messageAreaHeight: Int = 2,
2222
val messageAreaBackground: Color = Color.gray.darker.darker
23-
) {
23+
):
2424
import introprog.PixelWindow
2525

2626
/** Called when a key is pressed. Override if you want non-empty action.
@@ -85,78 +85,63 @@ abstract class BlockGame(
8585
* It draws only updated blocks aiming at the desired frame rate.
8686
* It calls each `onXXX` method if a corresponding event is detected.
8787
*/
88-
protected def gameLoop(stopWhen: => Boolean): Unit = while !stopWhen do {
88+
protected def gameLoop(stopWhen: => Boolean): Unit = while !stopWhen do
8989
import PixelWindow.Event
9090
val t0 = System.currentTimeMillis
9191
pixelWindow.awaitEvent(MaxWaitForEventMillis.toLong)
92-
while pixelWindow.lastEventType != PixelWindow.Event.Undefined do {
93-
pixelWindow.lastEventType match {
92+
while pixelWindow.lastEventType != PixelWindow.Event.Undefined do
93+
pixelWindow.lastEventType match
9494
case Event.KeyPressed => onKeyDown(pixelWindow.lastKey)
9595
case Event.KeyReleased => onKeyUp(pixelWindow.lastKey)
9696
case Event.WindowClosed => onClose()
9797
case Event.MousePressed => onMouseDown(pixelWindow.lastMousePos)
9898
case Event.MouseReleased => onMouseUp(pixelWindow.lastMousePos)
9999
case _ =>
100-
}
101100
pixelWindow.awaitEvent(1)
102-
}
103101
gameLoopAction()
104102
drawUpdatedBlocks()
105103
val elapsed = System.currentTimeMillis - t0
106-
if (gameLoopDelayMillis - elapsed) < MaxWaitForEventMillis then {
104+
if (gameLoopDelayMillis - elapsed) < MaxWaitForEventMillis then
107105
onFrameTimeOverrun(elapsed)
108-
}
109106
Thread.sleep((gameLoopDelayMillis - elapsed) max 0)
110-
}
111107

112108
/** Draw updated blocks and carry out post-update actions if any. */
113-
private def drawUpdatedBlocks(): Unit = {
114-
for x <- blockBuffer.indices do {
115-
for y <- blockBuffer(x).indices do {
116-
if isBufferUpdated(x)(y) then {
109+
private def drawUpdatedBlocks(): Unit =
110+
for x <- blockBuffer.indices do
111+
for y <- blockBuffer(x).indices do
112+
if isBufferUpdated(x)(y) then
117113
val pwx = x * blockSize
118114
val pwy = y * blockSize
119115
pixelWindow.fill(pwx, pwy, blockSize, blockSize, blockBuffer(x)(y))
120116
isBufferUpdated(x)(y) = false
121-
}
122-
}
123-
}
124117
toDoAfterBlockUpdates.foreach(_.apply())
125118
toDoAfterBlockUpdates.clear()
126-
}
127119

128120
/** Erase all blocks to background color. */
129-
def clearWindow(): Unit = {
121+
def clearWindow(): Unit =
130122
pixelWindow.clear()
131123
clearMessageArea()
132-
for x <- blockBuffer.indices do {
133-
for y <- blockBuffer(x).indices do {
124+
for x <- blockBuffer.indices do
125+
for y <- blockBuffer(x).indices do
134126
blockBuffer(x)(y) = background
135-
}
136-
}
137-
}
138127

139128
/** Paint a block in color `c` at (`x`,`y`) in block coordinates. */
140-
def drawBlock(x: Int, y: Int, c: Color): Unit = {
141-
if blockBuffer(x)(y) != c then {
129+
def drawBlock(x: Int, y: Int, c: Color): Unit =
130+
if blockBuffer(x)(y) != c then
142131
blockBuffer(x)(y) = c
143132
isBufferUpdated(x)(y) = true
144-
}
145-
}
146133

147134
/** Erase the block at (`x`,`y`) to `background` color. */
148-
def eraseBlock(x: Int, y: Int): Unit = {
149-
if blockBuffer(x)(y) != background then {
135+
def eraseBlock(x: Int, y: Int): Unit =
136+
if blockBuffer(x)(y) != background then
150137
blockBuffer(x)(y) = background
151138
isBufferUpdated(x)(y) = true
152-
}
153-
}
154139

155140
/** Write `msg` in `color` in the middle of the window.
156141
* The drawing is postponed until the end of the current game loop
157142
* iteration and thus the text drawn on top of any updated blocks.
158143
*/
159-
def drawCenteredText(msg: String, color: Color = pixelWindow.foreground, size: Int = blockSize): Unit = {
144+
def drawCenteredText(msg: String, color: Color = pixelWindow.foreground, size: Int = blockSize): Unit =
160145
toDoAfterBlockUpdates.append( () =>
161146
pixelWindow.drawText(
162147
msg,
@@ -165,23 +150,19 @@ abstract class BlockGame(
165150
size
166151
)
167152
)
168-
}
169153

170154
/** Write `msg` in `color` in the message area at ('x','y') in block coordinates. */
171-
def drawTextInMessageArea(msg: String, x: Int, y: Int, color: Color = pixelWindow.foreground, size: Int = blockSize): Unit = {
155+
def drawTextInMessageArea(msg: String, x: Int, y: Int, color: Color = pixelWindow.foreground, size: Int = blockSize): Unit =
172156
require(y < messageAreaHeight && y >= 0, s"not in message area: y = $y")
173157
require(x < dim._1 * blockSize && x >= 0, s"not in message area: x = $x")
174158
pixelWindow.drawText(msg, x * blockSize, (y + dim._2) * blockSize, color, size)
175-
}
176159

177160
/** Clear a rectangle in the message area in block coordinates. */
178-
def clearMessageArea(x: Int = 0, y: Int = 0, width: Int = dim._1, height: Int = messageAreaHeight): Unit = {
161+
def clearMessageArea(x: Int = 0, y: Int = 0, width: Int = dim._1, height: Int = messageAreaHeight): Unit =
179162
require(y < messageAreaHeight && y >= 0, s"not in message area: y = $y")
180163
require(x < dim._1 * blockSize && x >= 0, s"not in message area: x = $x")
181164
pixelWindow.fill(
182165
x * blockSize, (y + dim._2) * blockSize,
183166
width * blockSize, messageAreaHeight * blockSize + blockSize / 2,
184167
messageAreaBackground
185168
)
186-
}
187-
}

src/main/scala/introprog/Dialog.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package introprog
22

33
/** A module with utilities for creating standard GUI dialogs. */
4-
object Dialog {
4+
object Dialog:
55
import javax.swing.{JFileChooser, JOptionPane, JColorChooser}
66

77
Swing.init() // get platform-specific look and feel
88

99
/** Show a file choice dialog starting in `startDir` with confirm `button` text. */
10-
def file(button: String = "Open", startDir: String = "~"): String = {
10+
def file(button: String = "Open", startDir: String = "~"): String =
1111
val fs = new JFileChooser(new java.io.File(startDir))
12-
fs.showDialog(null, button) match {
12+
fs.showDialog(null, button) match
1313
case JFileChooser.APPROVE_OPTION => Option(fs.getSelectedFile.toString).getOrElse("")
1414
case _ => ""
15-
}
16-
}
1715

1816
/** Show a dialog with a `message` text. */
1917
def show(message: String): Unit = JOptionPane.showMessageDialog(null, message)
@@ -45,4 +43,3 @@ object Dialog {
4543
default: java.awt.Color = java.awt.Color.red
4644
): java.awt.Color =
4745
Option(JColorChooser.showDialog(null, message, default)).getOrElse(default)
48-
}

src/main/scala/introprog/IO.scala

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
package introprog
22

33
/** A module with input/output operations from/to the underlying file system. */
4-
object IO {
4+
object IO:
55
/**
66
* Load a string from a text file called `fileName` using encoding `enc`.
77
*
88
* @param fileName the path of the file.
99
* @param enc the encoding of the file.
1010
* @return the content loaded from the file.
1111
* */
12-
def loadString(fileName: String, enc: String = "UTF-8"): String = {
12+
def loadString(fileName: String, enc: String = "UTF-8"): String =
1313
var result: String = ""
1414
val source = scala.io.Source.fromFile(fileName, enc)
1515
try result = source.mkString finally source.close()
1616
result
17-
}
1817

1918
/**
2019
* Load string lines from a text file called `fileName` using encoding `enc`.
2120
*
2221
* @param fileName the path of the file.
2322
* @param enc the encoding of the file.
2423
* */
25-
def loadLines(fileName: String, enc: String = "UTF-8"): Vector[String] = {
24+
def loadLines(fileName: String, enc: String = "UTF-8"): Vector[String] =
2625
var result = Vector.empty[String]
2726
val source = scala.io.Source.fromFile(fileName, enc)
2827
try result = source.getLines().toVector finally source.close()
2928
result
30-
}
3129

3230
/**
3331
* Save `text` to a text file called `fileName` using encoding `enc`.
@@ -36,11 +34,10 @@ object IO {
3634
* @param fileName the path of the file.
3735
* @param enc the encoding of the file.
3836
* */
39-
def saveString(text: String, fileName: String, enc: String = "UTF-8"): Unit = {
37+
def saveString(text: String, fileName: String, enc: String = "UTF-8"): Unit =
4038
val f = new java.io.File(fileName)
4139
val pw = new java.io.PrintWriter(f, enc)
4240
try pw.write(text) finally pw.close()
43-
}
4441

4542
/**
4643
* Save `lines` to a text file called `fileName` using encoding `enc`.
@@ -58,23 +55,21 @@ object IO {
5855
* @param fileName the path of the file.
5956
* @return the serialized object.
6057
* */
61-
def loadObject[T](fileName: String): T = {
58+
def loadObject[T](fileName: String): T =
6259
val f = new java.io.File(fileName)
6360
val ois = new java.io.ObjectInputStream(new java.io.FileInputStream(f))
6461
try ois.readObject.asInstanceOf[T] finally ois.close()
65-
}
6662

6763
/**
6864
* Serialize `obj` to a binary file called `fileName`.
6965
*
7066
* @param obj the object to be serialized.
7167
* @param fileName the path of the file.
7268
* */
73-
def saveObject[T](obj: T, fileName: String): Unit = {
69+
def saveObject[T](obj: T, fileName: String): Unit =
7470
val f = new java.io.File(fileName)
7571
val oos = new java.io.ObjectOutputStream(new java.io.FileOutputStream(f))
7672
try oos.writeObject(obj) finally oos.close()
77-
}
7873

7974
/**
8075
* Test if a file with name `fileName` exists.
@@ -123,20 +118,18 @@ object IO {
123118
* @param from the path of the file to be moved.
124119
* @param to the path the file will be moved to.
125120
* */
126-
def move(from: String, to: String): Unit = {
121+
def move(from: String, to: String): Unit =
127122
import java.nio.file.{Files, Paths, StandardCopyOption}
128123
Files.move(Paths.get(from), Paths.get(to), StandardCopyOption.REPLACE_EXISTING)
129-
}
130124

131125
/**
132126
* Deletes `fileName`.
133127
*
134128
* @param fileName the path the file that will be deleted.
135129
* */
136-
def delete(fileName: String): Unit = {
130+
def delete(fileName: String): Unit =
137131
import java.nio.file.{Files, Paths}
138132
Files.delete(Paths.get(fileName))
139-
}
140133

141134
/**
142135
* Load image from file.
@@ -214,4 +207,3 @@ object IO {
214207

215208

216209

217-
}

0 commit comments

Comments
 (0)