Skip to content

Commit

Permalink
add options menu (read from config)
Browse files Browse the repository at this point in the history
  • Loading branch information
elkangaroo committed Jun 9, 2024
1 parent 41bf345 commit 03fad57
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 6 deletions.
54 changes: 54 additions & 0 deletions lib/GuiManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ function GuiManager:draw()
[ObjectType.BUTTON] = function(obj)
RenderManager:drawText(obj.text, obj.pos1, obj.flags)
end,
[ObjectType.SCROLLBAR] = function(obj)
RenderManager:drawOverlay(obj.pos1, obj.pos1 + Vector2d(210, 25), { 0, 0, 0, 0.5 })
RenderManager:drawImage("res/gfx/scrollbar.bmp", obj.pos1 + Vector2d(obj.pos2.x * 200, 0))
end,
[ObjectType.EDITBOX] = function(obj)
local fontSize = FONT_WIDTH_NORMAL
if bit.band(obj.flags, TF_SMALL_FONT) ~= 0 then
fontSize = FONT_WIDTH_SMALL
end

RenderManager:drawOverlay(obj.pos1, obj.pos1 + Vector2d(10 + obj.length * fontSize, 10 + fontSize), { 0, 0, 0, 0.5 })
RenderManager:drawText(obj.text, obj.pos1 + Vector2d(5, 5), obj.flags)
end,
[ObjectType.SELECTBOX] = function(obj)
local fontSize = FONT_WIDTH_NORMAL + LINE_SPACER_NORMAL
if bit.band(obj.flags, TF_SMALL_FONT) ~= 0 then
fontSize = FONT_WIDTH_SMALL + LINE_SPACER_SMALL
end

RenderManager:drawOverlay(obj.pos1, obj.pos2, { 0, 0, 0, 0.5 })

for i, entry in ipairs(obj.entries) do
if i == obj.selected then
RenderManager:drawText(entry, Vector2d(5 + obj.pos1.x, 5 + obj.pos1.y + ((i - 1) * fontSize)), bit.bor(obj.flags, TF_HIGHLIGHT))
else
RenderManager:drawText(entry, Vector2d(5 + obj.pos1.x, 5 + obj.pos1.y + ((i - 1) * fontSize)), obj.flags)
end
end

RenderManager:drawImage("res/gfx/pfeil_oben.bmp", Vector2d(obj.pos2.x - 27, obj.pos1.y + 3))
RenderManager:drawImage("res/gfx/pfeil_unten.bmp", Vector2d(obj.pos2.x - 27, obj.pos2.y - 27))
end,
}

while not Queue.isEmpty(self.queue) do
Expand Down Expand Up @@ -83,6 +115,7 @@ function GuiManager:addButton(position, text, flags)
if bit.band(flags, TF_SMALL_FONT) ~= 0 then
fontSize = FONT_WIDTH_SMALL
end

local tolerance = 0

-- React to mouse input.
Expand All @@ -105,6 +138,27 @@ function GuiManager:addButton(position, text, flags)
return clicked
end

-- Vector2d position, number value
function GuiManager:addScrollbar(position, value)
local scrollpos = Vector2d(value, 0)
-- value = value > 0.f ? (value < 1.f ? value : 1.f) : 0.f;
Queue.push(self.queue, { type = ObjectType.SCROLLBAR, pos1 = position, pos2 = scrollpos })
end

-- Vector2d position, number length, string text, number cursorPosition, number flags
function GuiManager:addEditbox(position, length, text, cursorPosition, flags)
flags = flags or TF_NORMAL

Queue.push(self.queue, { type = ObjectType.EDITBOX, pos1 = position, pos2 = cursorPosition, length = length, text = text, flags = flags })
end

-- Vector2d pos1, Vector2d pos2, table<string> entries, number selected, number flags
function GuiManager:addSelectbox(pos1, pos2, entries, selected, flags)
flags = flags or TF_NORMAL

Queue.push(self.queue, { type = ObjectType.SELECTBOX, pos1 = pos1, pos2 = pos2, entries = entries, selected = selected, flags = flags })
end

-- Vector2d position, string text, number flags
function GuiManager:__getTextPosition(position, text, flags)
local fontSize = FONT_WIDTH_NORMAL
Expand Down
4 changes: 4 additions & 0 deletions lib/RenderManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ end

-- string filename, Vector2d position
function RenderManager:drawImage(filename, position)
love.graphics.push("all")

local image = love.graphics.newImage(newImageDataWithBlackColorKey(filename))
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(image, position.x, position.y)

love.graphics.pop()
end

-- Vector2d pos1, Vector2d pos2, table<Color> color
Expand Down
2 changes: 1 addition & 1 deletion lib/SoundManager.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local SoundManager = {
isMuted = false,
isMuted = true,
sources = {},
}

Expand Down
6 changes: 3 additions & 3 deletions lib/states/MainMenuState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ function MainMenuState:update(dt)
app.state:switchState(LocalGameState())
end

-- if GuiManager:addButton(Vector2d(34, 420), "options") then
-- app.state:switchState(OptionState())
-- end
if GuiManager:addButton(Vector2d(34, 420), "options") then
app.state:switchState(OptionsMenuState())
end

-- if GuiManager:addButton(Vector2d(34, 460), "watch replay") then
-- app.state:switchState(ReplaySelectionState())
Expand Down
140 changes: 140 additions & 0 deletions lib/states/OptionsMenuState.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
local OptionsMenuState = {}
OptionsMenuState.__index = OptionsMenuState

setmetatable(OptionsMenuState, {
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:__construct(...)
return self
end
})

function OptionsMenuState:__construct()
love.mouse.setVisible(true)

GameConfig.load("conf/" .. app.options.config)

local scriptNameLeft = GameConfig.get("left_script_name")
local scriptNameRight = GameConfig.get("right_script_name")

self.scriptNames = { "human", "axji-0-2", "com_11", "gintonicV9", "hyp014", "reduced", "Union" }

self.playerOptions = {
[LEFT_PLAYER] = 1,
[RIGHT_PLAYER] = 1,
}

for i, name in ipairs(self.scriptNames) do
if name == scriptNameLeft then
self.playerOptions[LEFT_PLAYER] = i
end
if name == scriptNameRight then
self.playerOptions[RIGHT_PLAYER] = i
end
end

if GameConfig.getBoolean("left_player_human") then
self.playerOptions[LEFT_PLAYER] = 1
end

if GameConfig.getBoolean("right_player_human") then
self.playerOptions[RIGHT_PLAYER] = 1
end

self.playerName = {
[LEFT_PLAYER] = GameConfig.get("left_player_name"),
[RIGHT_PLAYER] = GameConfig.get("right_player_name"),
}

self.playerPosition = {
[LEFT_PLAYER] = 0,
[RIGHT_PLAYER] = 0,
}

self.botStrength = {
[LEFT_PLAYER] = GameConfig.getNumber("left_script_strength"),
[RIGHT_PLAYER] = GameConfig.getNumber("right_script_strength"),
}
end

function OptionsMenuState:update(dt)
GuiManager:addImage(Vector2d(0, 0), "res/gfx/backgrounds/strand2.bmp")
GuiManager:addOverlay(Vector2d(0, 0), Vector2d(800, 600))

GuiManager:addEditbox(Vector2d(5, 10), 15, self.playerName[LEFT_PLAYER], self.playerPosition[LEFT_PLAYER])
GuiManager:addEditbox(Vector2d(425, 10), 15, self.playerName[RIGHT_PLAYER], self.playerPosition[RIGHT_PLAYER])

GuiManager:addSelectbox(Vector2d(5, 50), Vector2d(375, 300), self.scriptNames, self.playerOptions[LEFT_PLAYER])
GuiManager:addSelectbox(Vector2d(425, 50), Vector2d(795, 300), self.scriptNames, self.playerOptions[RIGHT_PLAYER])

GuiManager:addText(Vector2d(400, 310), "bot strength", TF_ALIGN_CENTER)

local f = 1 - self.botStrength[LEFT_PLAYER] / MAX_BOT_DELAY
GuiManager:addScrollbar(Vector2d(15, 350), f)
-- mBotStrength[0] = static_cast<unsigned int> ((1.f-f) * MAX_BOT_DELAY + 0.5f);
local botStrengthLeftText = self:__getBotStrengthText(f)
GuiManager:addText(Vector2d(235, 350), botStrengthLeftText)

local f = 1 - self.botStrength[RIGHT_PLAYER] / MAX_BOT_DELAY
GuiManager:addScrollbar(Vector2d(440, 350), f)
-- mBotStrength[1] = static_cast<unsigned int> ((1.f - f) * MAX_BOT_DELAY + 0.5f);
local botStrengthRightText = self:__getBotStrengthText(f)
GuiManager:addText(Vector2d(660, 350), botStrengthRightText)

-- if GuiManager:addButton(Vector2d(40, 390), "input options") then
-- self:save()
-- app.state:switchState(InputOptionsState())
-- end

-- if GuiManager:addButton(Vector2d(40, 430), "graphic options") then
-- self:save()
-- app.state:switchState(GraphicOptionsState())
-- end

-- if GuiManager:addButton(Vector2d(40, 470), "misc options") then
-- self:save()
-- app.state:switchState(MiscOptionsState())
-- end

if GuiManager:addButton(Vector2d(224, 530), "ok") then
self:save()
app.state:switchState(MainMenuState())
end

if GuiManager:addButton(Vector2d(424, 530), "cancel") then
app.state:switchState(MainMenuState())
end
end

function OptionsMenuState:save()
print("saving options")
end

function OptionsMenuState:draw()
GuiManager:draw()
end

-- KeyConstant key
function OptionsMenuState:keypressed(key)
end

-- KeyConstant key
function OptionsMenuState:keyreleased(key)
end

function OptionsMenuState:getStateName()
return "OptionsMenuState"
end

-- number f
function OptionsMenuState:__getBotStrengthText(f)
if f > 0.66 then
return "strong"
elseif f > 0.33 then
return "medium"
else
return "weak"
end
end

return OptionsMenuState
8 changes: 6 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ LuaApiSandbox = require("lib.LuaApiSandbox")

State = require("lib.states.State")
MainMenuState = require("lib.states.MainMenuState")
OptionsMenuState = require("lib.states.OptionsMenuState")
GameState = require("lib.states.GameState")
LocalGameState = require("lib.states.LocalGameState")

Expand Down Expand Up @@ -95,6 +96,8 @@ TEMP_RULES_NAME = "server_rules.lua"
-- RenderManager.h
FONT_WIDTH_NORMAL = 24
FONT_WIDTH_SMALL = 12
LINE_SPACER_NORMAL = 6 -- Extra space between 2 lines in a normal SelectBox.
LINE_SPACER_SMALL = 3 -- Extra space between 2 lines in a small SelectBox.

-- RenderManager.h Text Flags (usable for the RenderManager:drawText() flag parameter)
TF_NORMAL = 0x00 -- 0 == false (backward compatibility for state modules)
Expand All @@ -108,6 +111,9 @@ TF_ALIGN_RIGHT = 0x10 -- Text aligned right
-- ScriptedInputSource.h
WAITING_TIME = 1500 -- The time the bot waits after game start

-- OptionsState.cpp
MAX_BOT_DELAY = 25 -- 25 frames = 0.33s (gamespeed: normal)

app = {}
app._VERSION = "0.1.0"
app._MIN_GAME_FPS = 5
Expand Down Expand Up @@ -155,8 +161,6 @@ function love.load(arg, unfilteredArg)
GameConfig.load("conf/" .. app.options.config)

if app.options.headless then
SoundManager.isMuted = true

app.state = LocalGameState()

return
Expand Down
Binary file added res/gfx/pfeil_links.bmp
Binary file not shown.
Binary file added res/gfx/pfeil_oben.bmp
Binary file not shown.
Binary file added res/gfx/pfeil_rechts.bmp
Binary file not shown.
Binary file added res/gfx/pfeil_unten.bmp
Binary file not shown.
Binary file added res/gfx/scrollbar.bmp
Binary file not shown.

0 comments on commit 03fad57

Please sign in to comment.