Skip to content

Commit

Permalink
Simple message windows allow options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Tomes committed Dec 22, 2023
1 parent 1281dc9 commit 419e7df
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/config/constants.ms
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
PIXEL_WIDTH = 960
PIXEL_HEIGHT = 640
TILE_SIZE = 16
TILE_DISPLAY_WIDTH = PIXEL_WIDTH / TILE_SIZE
TILE_DISPLAY_HEIGHT = PIXEL_HEIGHT / TILE_SIZE
TILE_DISPLAY_WIDTH = floor(PIXEL_WIDTH / TILE_SIZE)
TILE_DISPLAY_HEIGHT = floor(PIXEL_HEIGHT / TILE_SIZE)
TILE_DISPLAY_XMAX = TILE_DISPLAY_WIDTH - 1
TILE_DISPLAY_YMAX = TILE_DISPLAY_HEIGHT - 1
TILE_INDEX_SOLID = 219

UI_FONT_WIDTH = 16
UI_FONT_HEIGHT = 24
UI_DISPLAY_WIDTH = PIXEL_WIDTH / UI_FONT_WIDTH
UI_DISPLAY_HEIGHT = PIXEL_HEIGHT / UI_FONT_HEIGHT
UI_DISPLAY_WIDTH = floor(PIXEL_WIDTH / UI_FONT_WIDTH)
UI_DISPLAY_HEIGHT = floor(PIXEL_HEIGHT / UI_FONT_HEIGHT)
UI_DISPLAY_XMAX = UI_DISPLAY_WIDTH - 1
UI_DISPLAY_YMAX = UI_DISPLAY_HEIGHT - 1

9 changes: 8 additions & 1 deletion src/factories/entities.ms
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ makeSimpleVillager = function(map, pnt)
end function
e.examineOverride = function()
ui.showMessage(phrases.getRandom(), e.name)
message = phrases.getRandom()
options = [
"That's nice.",
"Bugger off.",
"You don't say?",
]
result = ui.showMessage(message, options, e.name)
Service.messages.report("You said: '{0}'".fill([ options[result] ]))
return true
end function
Expand Down
4 changes: 2 additions & 2 deletions src/ui.ms
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ examine = function(entities)
return (new ui.ExamineWindow).examine(entities)
end function
showMessage = function(message, title="Message")
ui.MessageWindow.make(message, title).show()
showMessage = function(message, options, title="Message")
return ui.MessageWindow.make(message, options, title).show()
end function
56 changes: 53 additions & 3 deletions src/ui/MessageWindow.ms
Original file line number Diff line number Diff line change
@@ -1,32 +1,82 @@
MessageWindow = {}

MessageWindow.make = function(message, title)
MessageWindow.make = function(message, options, title)
wnd = (new MessageWindow)
wnd.bounds = rect.make(4, 4, constants.UI_DISPLAY_WIDTH - 4 * 2, constants.UI_DISPLAY_HEIGHT - 4 * 2)
wnd.title = title
wnd.message = message

wnd.selectedOptionIndex = 0
if options == null or options.len == 0 then
wnd.options = [ "OK" ]
else
wnd.options = options
end if

return wnd
end function

MessageWindow.drawOption = function(optionIndex, isSelected)
// a=Color.white
// b=Color.black
innerBounds = self.messageBounds
text = self.options[optionIndex]
yDelta = self.options.len - optionIndex - 2

// c=Color.blue.light
Display.hud.print(text, innerBounds.left + 2, innerBounds.top + 1 + yDelta, Color.white, Color.black)

if isSelected then
Display.hud.print(">", innerBounds.left, innerBounds.top + 1 + yDelta, Color.blue.light, Color.black)
end if
end function

MessageWindow.drawOptions = function()
for n in range(self.options.len - 1)
isSelected = (self.selectedOptionIndex == n)
// globals.err=[self.selectedOptionIndex, isSelected, n]
self.drawOption(n, isSelected)
end for
end function

MessageWindow.draw = function()
ui.drawWindow(Display.hud, self.bounds, self.title)
lineCount = ui.drawText(Display.hud, self.message, self.messageBounds)

self.drawOptions()
end function

// Returns the index of the selected item.
MessageWindow.show = function()
self.draw
self.draw()

result = null
while true
delta = 0
k = key.get.code
if keybindings.exit.contains(k) then
if keybindings.up.contains(k) then
delta = -1
else if keybindings.down.contains(k) then
delta = 1
else if keybindings.exit.contains(k) then
result = -1
break
else if keybindings.select.contains(k) then
result = self.selectedOptionIndex
break
end if

if delta != 0 then
self.selectedOptionIndex += delta
if self.selectedOptionIndex < 0 or self.selectedOptionIndex >= self.options.len then
self.selectedOptionIndex -= delta
else
self.draw()
end if
end if
end while

return result
end function

MessageWindow.messageBounds = function()
Expand Down

0 comments on commit 419e7df

Please sign in to comment.