-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #848 from uspgamedev/feature/user-preferences
Settings Menu
- Loading branch information
Showing
8 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"bgm-volume": { | ||
"default": 100, | ||
"range": [0, 100], | ||
"step": 10 | ||
}, | ||
"sfx-volume": { | ||
"default": 100, | ||
"range": [0, 100], | ||
"step": 10 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
local DB = require 'database' | ||
local INPUT = require 'input' | ||
local DIRECTIONALS = require 'infra.dir' | ||
local PROFILE = require 'infra.profile' | ||
local PLAYSFX = require 'helpers.playsfx' | ||
local SettingsView = require 'view.settings' | ||
|
||
local state = {} | ||
|
||
local insert = table.insert | ||
local sort = table.sort | ||
local max = math.max | ||
local min = math.min | ||
local unpack = unpack | ||
|
||
local _selection | ||
local _schema | ||
local _fields | ||
local _fieldcount | ||
local _original | ||
local _view | ||
local _save | ||
|
||
local function _changeField(field, offset) | ||
local low, high = unpack(_schema[field]["range"]) | ||
local step = _schema[field]["step"] | ||
local value = (_changes[field] or _original[field]) + offset * step | ||
_changes[field] = min(high, max(low, value)) | ||
PROFILE.setPreference(field, _changes[field]) | ||
end | ||
|
||
function state:init() | ||
_schema = DB.loadSetting("user-preferences") | ||
_fields = {} | ||
for field in pairs(_schema) do | ||
insert(_fields, field) | ||
end | ||
sort(_fields) -- show them in alphabetical order! | ||
_fieldcount = #_fields | ||
_selection = 1 | ||
end | ||
|
||
function state:enter(from, ...) | ||
_selection = 1 | ||
_original = {} | ||
for _,field in ipairs(_fields) do | ||
_original[field] = PROFILE.getPreference(field) or _schema[field].default | ||
PROFILE.setPreference(field, _original[field]) | ||
end | ||
_changes = setmetatable({}, { __index = original }) | ||
_view = SettingsView(_fields) | ||
_view:addElement("GUI") | ||
end | ||
|
||
function state:update(dt) | ||
if INPUT.wasActionPressed("CONFIRM") then | ||
PLAYSFX 'ok-menu' | ||
_save = true | ||
SWITCHER.pop() | ||
elseif INPUT.wasActionPressed("CANCEL") then | ||
PLAYSFX 'back-menu' | ||
_save = false | ||
SWITCHER.pop() | ||
elseif DIRECTIONALS.wasDirectionTriggered("UP") then | ||
PLAYSFX 'select-menu' | ||
_selection = (_selection - 2 + _fieldcount) % _fieldcount + 1 | ||
_view:setFocus(_selection) | ||
elseif DIRECTIONALS.wasDirectionTriggered("DOWN") then | ||
PLAYSFX 'select-menu' | ||
_selection = (_selection % _fieldcount) + 1 | ||
_view:setFocus(_selection) | ||
elseif DIRECTIONALS.wasDirectionTriggered("LEFT") then | ||
PLAYSFX 'ok-menu' | ||
_changeField(_fields[_selection], -1) | ||
elseif DIRECTIONALS.wasDirectionTriggered("RIGHT") then | ||
PLAYSFX 'ok-menu' | ||
_changeField(_fields[_selection], 1) | ||
end | ||
end | ||
|
||
function state:leave() | ||
-- when you leave, it will either save or restore the changes | ||
if _save then | ||
PROFILE.save() | ||
PROFILE.init() | ||
else | ||
for field, value in ipairs(_original) do | ||
PROFILE.setPreference(field, value) | ||
end | ||
end | ||
_view:destroy() | ||
end | ||
|
||
function state:draw() | ||
return Draw.allTables() | ||
end | ||
|
||
return state | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
local COLORS = require 'domain.definitions.colors' | ||
local PROFILE = require 'infra.profile' | ||
local FONT = require 'view.helpers.font' | ||
|
||
local fmod = math.fmod | ||
local sin = math.sin | ||
|
||
local _PI = math.pi | ||
|
||
local _font | ||
|
||
local SettingsView = Class{ | ||
__includes = { ELEMENT } | ||
} | ||
|
||
local _getPreference = PROFILE.getPreference | ||
|
||
function SettingsView:init(fields) | ||
ELEMENT.init(self) | ||
self.fields = fields | ||
self.focus = 1 | ||
_font = FONT.get("Text", 20) | ||
end | ||
|
||
function SettingsView:setFocus(idx) | ||
self.focus = idx | ||
end | ||
|
||
function SettingsView:draw() | ||
local g = love.graphics | ||
local focus = self.focus | ||
local width = 256 | ||
local height = 10 | ||
local mx, my = 8, 8 | ||
local font_height = 20 | ||
local lw = 4 | ||
local base_y = 256 | ||
local x = 320 | ||
-- draw one settings input type | ||
FONT.set("Title", 32) | ||
g.setColor(COLORS.NEUTRAL) | ||
g.print("SETTINGS", x, base_y - 80) | ||
_font:set() | ||
for i, field in ipairs(self.fields) do | ||
local y = base_y + (i - 1) * (height + 4*my + font_height) | ||
local percentage = _getPreference(field) / 100 | ||
local is_focused = (i == focus) | ||
g.push() | ||
g.translate(x, y) | ||
|
||
-- field name | ||
g.setColor(is_focused and COLORS.NEUTRAL or COLORS.HALF_VISIBLE) | ||
g.print(field:gsub("[-]", " "):upper(), 0, -font_height) | ||
g.translate(0, font_height) | ||
|
||
-- line width offset | ||
g.translate(0, lw) | ||
|
||
-- trail | ||
g.setLineWidth(lw) | ||
g.setColor(is_focused and COLORS.EMPTY or COLORS.DARKER) | ||
g.line(0, 0, width, 0) | ||
|
||
-- value progression | ||
g.setColor(is_focused and COLORS.NEUTRAL or COLORS.HALF_VISIBLE) | ||
g.line(0, 0, percentage * width, 0) | ||
g.ellipse("fill", percentage * width, 0, height, height) | ||
|
||
g.pop() | ||
end | ||
end | ||
|
||
return SettingsView | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters