Skip to content

Commit

Permalink
UI uses TileDisplay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Tomes committed Dec 19, 2023
1 parent e9f4f18 commit bef2f1f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 106 deletions.
Binary file added assets/OEM437_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/micro-font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 97 additions & 32 deletions main.ms
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,72 @@ ensureImport "townGenerator"
ensureImport "mapgen"
ensureImport "fov"

ensureImport "constants"
ensureImport "keybindings"
ensureImport "settings"

Display = {}

HeadsUpDisplay = {}

HeadsUpDisplay.initTiles = function(displayNumber)
display(displayNumber).mode = displayMode.tile
display(displayNumber).tileSet = file.loadImage("assets/OEM437_8.png")
display(displayNumber).tileSetTileSize = 8
display(displayNumber).cellSize = 16
display(displayNumber).extent = [constants.TILE_DISPLAY_WIDTH, constants.TILE_DISPLAY_HEIGHT]
return display(displayNumber)
end function

HeadsUpDisplay.init = function(displayNumberFront, displayNumberBack)
self.display = self.initTiles(displayNumberFront)
self.displayBack = self.initTiles(displayNumberBack)
return self
end function

HeadsUpDisplay.setCellBackColor = function(x, y, backColor)
if backColor isa Color then
backColor = backColor.str()
end if
self.displayBack.setCell x, y, constants.TILE_INDEX_SOLID
self.displayBack.setCellTint x, y, backColor
end function

HeadsUpDisplay.setCell = function(x, y, tile, color="#FFFFFF", backColor = "#00000000")
if tile isa string then
tile = code(tile)
end if
if color isa Color then
color = color.str()
end if
self.display.setCell x, y, tile
self.display.setCellTint x, y, color
self.setCellBackColor x, y, backColor
end function

HeadsUpDisplay.clear = function()
for y in range(0, constants.TILE_DISPLAY_YMAX)
for x in range(0, constants.TILE_DISPLAY_XMAX)
self.setCell x, y, 0, color.black, color.clear
end for
end for
end function

HeadsUpDisplay.print = function(text, x, y, color="#FFFFFF", backColor="#00000000")
for ch in text
self.setCell x, y, code(ch), color, backColor
x += 1
end for
end function

Display.initialize = function()
HUD = 0
HUD_BACK = 1
PARTICLES_0 = 2
PARTICLES_1 = 3
MAP_0 = 4
MAP_1 = 5

activate = function(n)
display(n).mode = displayMode.text
display(n).delimiter = ""
Expand All @@ -89,40 +149,45 @@ Display.initialize = function()
return display(n)
end function

Display.hud = activate(0)
activateTiles = function(n)
return HeadsUpDisplay.init(n)
end function

// Display.hud = activate(0)
Display.hud = HeadsUpDisplay.init(HUD, HUD_BACK)

Display.particles = activate(1)
deactivate(1)
activate(2)
Display.particles = activate(PARTICLES_0)
deactivate(PARTICLES_0)
activate(PARTICLES_1)

Display.particles = display(1)
Display.particles = display(PARTICLES_0)
Display.flipParticles = function()
if display(1).mode == displayMode.off then
display(1).mode = displayMode.text
display(2).mode = displayMode.text
Display.particles = display(2)
display(2).mode = displayMode.off
if display(PARTICLES_0).mode == displayMode.off then
display(PARTICLES_0).mode = displayMode.text
display(PARTICLES_1).mode = displayMode.text
Display.particles = display(PARTICLES_1)
display(PARTICLES_1).mode = displayMode.off
else
display(1).mode = displayMode.text
display(2).mode = displayMode.text
Display.particles = display(1)
display(1).mode = displayMode.off
display(PARTICLES_0).mode = displayMode.text
display(PARTICLES_1).mode = displayMode.text
Display.particles = display(PARTICLES_0)
display(PARTICLES_0).mode = displayMode.off
end if
end function

Display.map = activate(3)
deactivate(3)
activate(4)
Display.map = activate(MAP_0)
deactivate(MAP_0)
activate(MAP_1)

Display.flipMap = function()
if display(3).mode == displayMode.off then
activate(3)
Display.map = activate(4)
deactivate(4)
if display(MAP_0).mode == displayMode.off then
activate(MAP_0)
Display.map = activate(MAP_1)
deactivate(MAP_1)
else
activate(4)
Display.map = activate(3)
deactivate(3)
activate(MAP_1)
Display.map = activate(MAP_0)
deactivate(MAP_0)
end if
end function
end function
Expand Down Expand Up @@ -167,20 +232,20 @@ drawEntities = function(display, map, renderOffset)
end function

drawHUD = function(display, player)
display.color = color.clear
display.backColor = color.clear
// display.color = color.clear
// display.backColor = color.clear
display.clear()
display.row = 26
// display.row = 26

display.column = 0
display.color = color.white
display.backColor = color.black
// display.column = 0
// display.color = color.white
// display.backColor = color.black

statusBar = "HP: {0} / {1} LVL: {2} XP: {3} / {4} world: {5}".fill([player.currentHP, player.maxHP, player.level, player.xp, player.xpToNextLevel, Service.world.currentLevel])
statusBar = statusBar + " " * (67 - statusBar.len)

display.print(statusBar)
display.print(statusBar, 0, 39, color.white, color.black)

Service.messages.update()
end function

Expand Down
8 changes: 3 additions & 5 deletions src/hud.ms
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ Message.make = function(text, foregroundColor = null, backgroundColor = null)
end function

Message.draw = function(row)
Display.hud.row = row
Display.hud.column = 0
ratio = self.lifeSpan / self.maxLifeSpan
Display.hud.color = color.lerp(color.clear, self.foregroundColor, ratio)
Display.hud.backColor = color.lerp(color.clear, self.backgroundColor, ratio)
Display.hud.print(self.text)
fg = color.lerp(color.clear, self.foregroundColor, ratio)
bg = color.lerp(color.clear, self.backgroundColor, ratio)
Display.hud.print self.text, 0, row, fg, bg
end function

MessageLog = {}
Expand Down
59 changes: 33 additions & 26 deletions src/ui.ms
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ DEFAULT_BACKGROUND_COLOR = color.black
drawCell = function(display, x, y, fg, bg, text)
if fg isa Color then fg = fg.str()
if bg isa Color then bg = bg.str()
display.setCell(x, y, text)
display.setCellColor(x, y, fg)
display.setCellBackColor(x, y, bg)

// display.setCell(x, y, text)
// display.setCellColor(x, y, fg)
// display.setCellBackColor(x, y, bg)

display.setCell x, y, text, fg, bg
end function

clearRect = function(display, bounds, text = null, foregroundColor = null, backgroundColor = null)
Expand Down Expand Up @@ -38,38 +41,42 @@ drawCenteredText = function(display, x1, x2, y, fg, bg, text)
if bg isa Color then bg = bg.str()

centerX = floor((x1 + x2) / 2)
display.row = y
display.column = centerX - floor(text.len / 2)
display.color = fg
display.backColor = bg
display.print(text)

// display.row = y
// display.column = centerX - floor(text.len / 2)
// display.color = fg
// display.backColor = bg
// display.print(text)

display.print text, centerX - floor(text.len / 2), y, fg, bg
end function

// Returns the number of lines printed.
drawText = function(display, text, bounds, fg, bg)
display.backColor = math.coalesce(bg, DEFAULT_BACKGROUND_COLOR)
display.color = math.coalesce(fg, DEFAULT_FOREGROUND_COLOR)
drawText = function(display, text, bounds, fg, bg=null)
color = math.coalesce(fg, DEFAULT_FOREGROUND_COLOR)
backColor = math.coalesce(bg, DEFAULT_BACKGROUND_COLOR)

lineCount = 0
while text
display.row = bounds.bottom - lineCount
display.column = bounds.x
x = bounds.x
y = bounds.bottom - lineCount

if text.len <= bounds.width then
display.print(text)
display.print text, x, y, color, backColor
return lineCount + 1
end if
foundCut = false
for i in range(bounds.width, 0)
if text[i] == " " then
display.print(text[:i])
display.print text[:i], x, y, color, backColor
text = text[i + 1:]
foundCut = true
break
end if
end for
if not foundCut then
// Couldn't find a space to cut on so, out of desperation, just cut at width.
display.print(text[:bounds.width])
display.print text[:bounds.width], x, y, color, backColor
text = text[bounds.width:]
end if
lineCount += 1
Expand All @@ -86,21 +93,21 @@ WINDOW_TITLE_BACKGROUND = color.black
drawWindow = function(display, bounds, title=null)
ui.clearRect(display, bounds)
drawVerticalLine(display, bounds.left, bounds.top + 1, bounds.bottom - 1, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "|")
drawVerticalLine(display, bounds.right, bounds.top + 1, bounds.bottom - 1, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "|")
drawHorizontalLine(display, bounds.left + 1, bounds.right - 1, bounds.top, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "-")
drawHorizontalLine(display, bounds.left + 1, bounds.right - 1, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "-")
drawVerticalLine(display, bounds.left, bounds.top + 1, bounds.bottom - 1, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 186)
drawVerticalLine(display, bounds.right, bounds.top + 1, bounds.bottom - 1, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 186)
drawHorizontalLine(display, bounds.left + 1, bounds.right - 1, bounds.top, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 205)
drawHorizontalLine(display, bounds.left + 1, bounds.right - 1, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 205)
drawCell(display, bounds.left, bounds.top, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "+")
drawCell(display, bounds.right, bounds.top, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "+")
drawCell(display, bounds.left, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "+")
drawCell(display, bounds.right, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "+")
drawCell(display, bounds.left, bounds.top, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 200)
drawCell(display, bounds.right, bounds.top, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 188)
drawCell(display, bounds.left, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 201)
drawCell(display, bounds.right, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 187)
if title != null then
title = " {0} ".fill([ title ])
halfLength = floor(title.len / 2)
drawCell(display, bounds.centerX - halfLength - 2, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "]")
drawCell(display, bounds.centerX - halfLength + title.len - 1, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, "[")
drawCell(display, bounds.centerX - halfLength - 2, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 181)
drawCell(display, bounds.centerX - halfLength + title.len - 1, bounds.bottom, WINDOW_BORDER_FOREGROUND, WINDOW_BORDER_BACKGROUND, 198)
drawCenteredText(display, bounds.left, bounds.right, bounds.bottom, WINDOW_TITLE_FOREGROUND, WINDOW_TITLE_BACKGROUND, title)
end if
Expand Down
13 changes: 5 additions & 8 deletions src/ui/ExamineSelectListItem.ms
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ ExamineSelectListItem.draw = function(parent, isSelected)
bg = parent.ITEM_BACKGROUND.str()
fg = parent.ITEM_FOREGROUND.str()
end if
parent.display.backColor = bg
parent.display.color = fg

x = parent.listBounds.left
y = parent.getItemRow(self.index)

self.entity.tile.draw(parent.display, x, y)

// The backgroundColor won't actually be visible. It'll be overwritten in that last for-loop.
parent.display.setCell x, y, self.entity.tile.char, self.entity.tile.foregroundColor, self.entity.tile.backgroundColor

parent.display.row = y
parent.display.column = x + 2
parent.display.print(self.entity.str())
parent.display.print self.entity.str(), x + 2, y, fg, bg

for x in range(parent.listBounds.left, parent.listBounds.right)
parent.display.setCellBackColor(x, y, bg)
parent.display.setCellBackColor x, y, bg
end for
end function

Expand Down
19 changes: 6 additions & 13 deletions src/ui/ExamineWindow.ms
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,18 @@ end function

ExamineWindow.reset = function(entities)
list = self.generateList(entities)
self.selectList.init(Display.hud, rect.make(4, 4, 68 - 4 * 2, 26 - 4 * 2))
self.selectList.init(Display.hud, rect.make(4, 4, constants.TILE_DISPLAY_WIDTH - 4 * 2, constants.TILE_DISPLAY_HEIGHT - 4 * 2))
self.selectList.reset(list)
end function

ExamineWindow.draw = function(title, description="")
ExamineWindow.draw = function(title) //, description="")
windowBounds = self.selectList.windowBounds
ui.drawWindow(Display.hud, windowBounds, title)
ui.drawVerticalLine(Display.hud, windowBounds.centerX, windowBounds.top + 1, windowBounds.bottom - 1, ui.WINDOW_BORDER_FOREGROUND, ui.WINDOW_BORDER_BACKGROUND, "|")
ui.drawVerticalLine(Display.hud, windowBounds.centerX, windowBounds.top + 1, windowBounds.bottom - 1, ui.WINDOW_BORDER_FOREGROUND, ui.WINDOW_BORDER_BACKGROUND, 179)
ui.drawCell(Display.hud, windowBounds.centerX, windowBounds.top, ui.WINDOW_BORDER_FOREGROUND, ui.WINDOW_BORDER_BACKGROUND, 207)

Display.hud.color = ui.WINDOW_BORDER_FOREGROUND
Display.hud.row = windowBounds.top
Display.hud.column = windowBounds.centerX
Display.hud.print("+")

Display.hud.backColor = color.black
Display.hud.color = color.white
Display.hud.row = windowBounds.bottom - 1
Display.hud.column = windowBounds.left + 1
Display.hud.print(description)
// Unused?
// Display.hud.print description, windowBounds.left + 1, windowBounds.bottom - 1, color.white, color.black

self.selectList.drawItems()
end function
Expand Down
17 changes: 9 additions & 8 deletions src/ui/InventorySelectListItem.ms
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ InventorySelectListItem.draw = function(parent, isSelected)
bg = parent.ITEM_BACKGROUND.str()
fg = parent.ITEM_FOREGROUND.str()
end if
parent.display.backColor = bg
parent.display.color = fg

x = parent.listBounds.left
y = parent.getItemRow(self.index)

item.tile.draw(parent.display, x, y)
// The backgroundColor won't actually be visible. It'll be overwritten in that last for-loop.
parent.display.setCell x, y, item.tile.char, item.tile.foregroundColor, item.tile.backgroundColor

parent.display.row = y
parent.display.column = x + 2
parent.display.print(stack.str())
// parent.display.row = y
// parent.display.column = x + 2
// parent.display.print(stack.str())
parent.display.print stack.str(), x + 2, y, fg, bg
if self.entity.isEquipped(item) then
parent.display.print(" [E]")
// parent.display.print(" [E]")
parent.display.print " [E]", x + 2 + stack.str().len, y, fg, bg
end if

for x in range(parent.listBounds.left, parent.listBounds.right)
parent.display.setCellBackColor(x, y, bg)
parent.display.setCellBackColor x, y, bg
end for
end function

Expand Down
Loading

0 comments on commit bef2f1f

Please sign in to comment.