Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode RenderOp as a sealed trait #15

Merged
merged 2 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions core/src/main/scala/eu/joaocosta/interim/RenderOp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,65 @@ package eu.joaocosta.interim
* For DrawText, the backend is expected to layout the text. However, there's a `asDrawChars` method that applies
* some naive layout logic and returns simpler [[RenderOp.DrawChar]] operations.
*/
enum RenderOp:
case DrawRect(area: Rect, color: Color)
case DrawText(
sealed trait RenderOp {
def area: Rect
def color: Color

def clip(rect: Rect): RenderOp
}

object RenderOp:
/** Operation to draw a rectangle on the screen.
*
* @param area area to render
* @param color color of the rectangle
*/
final case class DrawRect(area: Rect, color: Color) extends RenderOp:
def clip(rect: Rect): DrawRect = copy(area = area & rect)

/** Operation to draw text on the screen.
*
* @param area area to render, text outside this area should not be shown
* @param color text color
* @param text string to render
* @param fontSize font size in pixels
* @param textArea area where the text should be layed out
* @param horizontalAlignment how the text should be layed out horizontally
* @param verticalAlignment how the text should be layed out vertically
*/
final case class DrawText(
area: Rect,
color: Color,
text: String,
fontSize: Int,
textArea: Rect,
horizontalAlignment: TextLayout.HorizontalAlignment,
verticalAlignment: TextLayout.VerticalAlignment
)
case Custom[T](area: Rect, color: Color, data: T)
) extends RenderOp:
def clip(rect: Rect): DrawText = copy(area = area & rect)

object RenderOp:
/** Converts a DrawText operation into a sequence of simpler DrawChar operations.
*
* @param charWith function that, given a char, returns its width in pixels
* @param lineHeight line height to use, in pixels
*/
def asDrawChars(
charWidth: Char => Int = _ => fontSize,
lineHeight: Int = (fontSize * 1.3).toInt
): List[DrawChar] = TextLayout.asDrawChars(this, charWidth, lineHeight)

/** Operation to draw a custom element on the screen
*
* @param area area to render
* @param color fallback color
* @param data domain specific data to use when rendering this element
*/
final case class Custom[T](area: Rect, color: Color, data: T) extends RenderOp:
def clip(rect: Rect): Custom[T] = copy(area = area & rect)

/** Operation to draw a single character.
* Note that this is not part of the RenderOp enum. InterIm components will never generate this operation.
*
* The only way to get it is to call `DrawText#asDrawChars`.
*/
final case class DrawChar(area: Rect, color: Color, char: Char)

/** Converts a DrawText operation into a sequence of simpler DrawChar operations.
*
* @param charWith function that, given a char, returns its width in pixels
* @param lineHeight line height to use, in pixels
*/
extension (textOp: DrawText)
def asDrawChars(
charWidth: Char => Int = _ => textOp.fontSize,
lineHeight: Int = (textOp.fontSize * 1.3).toInt
): List[DrawChar] = TextLayout.asDrawChars(textOp, charWidth, lineHeight)

extension (renderOp: RenderOp)
def clip(rect: Rect): RenderOp =
renderOp match
case dr: DrawRect => dr.copy(area = dr.area & rect)
case dt: DrawText => dt.copy(area = dt.area & rect)
case c: Custom[_] => c.copy(area = c.area & rect)
2 changes: 1 addition & 1 deletion core/src/main/scala/eu/joaocosta/interim/api/Layouts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait Layouts:
if (area.isMouseOver) inputState
else inputState.copy(mouseX = Int.MinValue, mouseY = Int.MinValue)
val result = body(using newInputState, newUiState)
newUiState.ops.mapInPlace(_.clip(area))
newUiState.ops.mapInPlace(_.clip(area)).filterInPlace(!_.area.isEmpty)
uiState ++= newUiState
result

Expand Down