From 14939a87010ab17425d73b53829aa22c17c96d79 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Tue, 22 Jun 2021 03:38:08 +0500 Subject: Added Settings UI --- cwd/assets/altcraft/scripts/init.lua | 27 +----- cwd/assets/altcraft/scripts/ui.lua | 138 +++++++++++++++++++++++++++++ cwd/assets/altcraft/ui/main-menu.rml | 3 +- cwd/assets/altcraft/ui/mc-styles.rcss | 6 +- cwd/assets/altcraft/ui/options-styles.rcss | 84 ++++++++++++++++++ cwd/assets/altcraft/ui/options.rml | 61 +++++++++++++ cwd/assets/altcraft/ui/pause.rml | 3 +- 7 files changed, 294 insertions(+), 28 deletions(-) create mode 100644 cwd/assets/altcraft/scripts/ui.lua create mode 100644 cwd/assets/altcraft/ui/options-styles.rcss create mode 100644 cwd/assets/altcraft/ui/options.rml (limited to 'cwd') diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua index bf7739e..c626375 100644 --- a/cwd/assets/altcraft/scripts/init.lua +++ b/cwd/assets/altcraft/scripts/init.lua @@ -14,6 +14,7 @@ function plugin.onLoad () con:LoadDocument("altcraft/ui/main-menu"):Show() con:LoadDocument("altcraft/ui/hud") con:LoadDocument("altcraft/ui/pause") + con:LoadDocument("altcraft/ui/options") end function plugin.onChangeState (newState) @@ -41,33 +42,11 @@ function plugin.onUnload () AC.LogInfo("AC Core unloaded") end -local function UpdateHud() - local doc = {} - for i,d in ipairs(rmlui.contexts["default"].documents) do - if d.title == "Playing" then - doc = d - end - end - - local playerEnt = AC.GetGameState():GetPlayer() - doc:GetElementById('dbg-pos').inner_rml = string.format("%.1f %.1f %.1f", playerEnt.pos.x, playerEnt.pos.y, playerEnt.pos.z) - - local selection = AC.GetGameState():GetSelectionStatus() - if selection.isBlockSelected then - doc:GetElementById('dbg-select-pos').inner_rml = tostring(selection.selectedBlock) - else - doc:GetElementById('dbg-select-pos').inner_rml = "" - end - - local player = AC.GetGameState():GetPlayerStatus() - local playerHp = string.format("%.0f", player.health) - doc:GetElementById('status-hp').inner_rml = playerHp - doc:GetElementById('status-hp-bar'):SetAttribute("value", playerHp) -end +require("altcraft/ui") function plugin.onTick (deltaTime) + UpdateUi() if AC.GetGameState() and AC.GetGameState():GetPlayer() and AC.GetGameState():GetTimeStatus().worldAge > 0 then - UpdateHud() -- local player = AC.GetGameState():GetPlayer() -- player.pos.x = player.pos.x + deltaTime * 0.5 diff --git a/cwd/assets/altcraft/scripts/ui.lua b/cwd/assets/altcraft/scripts/ui.lua new file mode 100644 index 0000000..c55436f --- /dev/null +++ b/cwd/assets/altcraft/scripts/ui.lua @@ -0,0 +1,138 @@ +local options = { + brightness = 0.2, + flight = false, + mouseSensetivity = 0.1, + renderDistance = 2, + resolutionScale = 1.0, + targetFps = 60, + vsync = false, + wireframe = false +} + +function OpenOptions(doc) + optionsReturnDocument = doc + local optionsDoc = {} + for i,d in ipairs(rmlui.contexts["default"].documents) do + if d.title == "Options" then + optionsDoc = d + end + end + doc:Hide() + optionsDoc:Show() + optionsDoc.style["background-color"] = doc.style["background-color"] +end + +function CloseOptions(doc) + for i, v in pairs(options) do + local input = doc:GetElementById(i) + if type(v) == "number" then + local val = input:GetAttribute("value") + if i == "targetFps" and val == 301 then + AC.Settings.WriteDouble(i, 10000) + else + AC.Settings.WriteDouble(i, tonumber(val)) + end + elseif type(v) == "boolean" then + local val = input:HasAttribute("checked") + AC.Settings.WriteBool(i, val) + end + end + AC.Settings.Save() + AC.SettingsUpdate() + + optionsReturnDocument:Show() + doc:Hide() +end + +function OptionsDefaultHandler(event) + local input = event.current_element.previous_sibling + local id = input:GetAttribute("id") + if input:GetAttribute("type") == "checkbox" then + if options[id] then + input:SetAttribute("checked", "") + else + input:RemoveAttribute("checked") + end + else + input:SetAttribute("value", options[id]) + end +end + +function UpdateUi() + local doc = {} + local uiDoc = {} + for i,d in ipairs(rmlui.contexts["default"].documents) do + if d.title == "Playing" then + doc = d + elseif d.title == "Options" then + uiDoc = d + end + end + + if AC.GetGameState() and AC.GetGameState():GetPlayer() and AC.GetGameState():GetTimeStatus().worldAge > 0 then + local playerEnt = AC.GetGameState():GetPlayer() + doc:GetElementById('dbg-pos').inner_rml = string.format("%.1f %.1f %.1f", playerEnt.pos.x, playerEnt.pos.y, playerEnt.pos.z) + + local selection = AC.GetGameState():GetSelectionStatus() + if selection.isBlockSelected then + doc:GetElementById('dbg-select-pos').inner_rml = tostring(selection.selectedBlock) + else + doc:GetElementById('dbg-select-pos').inner_rml = "" + end + + local player = AC.GetGameState():GetPlayerStatus() + local playerHp = string.format("%.0f", player.health) + doc:GetElementById('status-hp').inner_rml = playerHp + doc:GetElementById('status-hp-bar'):SetAttribute("value", playerHp) + end + + + local uiInit = optionsListenersAdded == nil + if uiInit then + AC.Settings.Load() + end + + for i,v in pairs(options) do + local input = uiDoc:GetElementById(i) + local span = input.next_sibling + + if uiInit then + span:AddEventListener("click", OptionsDefaultHandler, true) + + if type(v) == "number" then + local val = AC.Settings.ReadDouble(i, v) + input:SetAttribute("value", tostring(val)) + elseif type(v) == "boolean" then + local val = AC.Settings.ReadBool(i, v) + if val then + input:SetAttribute("checked", "") + else + input:RemoveAttribute("checked") + end + end + end + + if type(v) == "number" then + local val = input:GetAttribute("value") + if v == math.floor(v) and i ~= "resolutionScale" then + span.inner_rml = string.format("%d (%d)", math.floor(val), v) + if i == "targetFps" and val == 301 then + span.inner_rml = string.format("∞ (%d)", v) + end + else + span.inner_rml = string.format("%.2f (%.2f)", val, v) + end + elseif type(v) == "boolean" then + if v then + span.inner_rml = "(on)" + else + span.inner_rml = "(off)" + end + end + end + + if uiInit == true then + optionsListenersAdded = true + AC.SettingsUpdate() + end +end diff --git a/cwd/assets/altcraft/ui/main-menu.rml b/cwd/assets/altcraft/ui/main-menu.rml index e796b67..0aaac89 100644 --- a/cwd/assets/altcraft/ui/main-menu.rml +++ b/cwd/assets/altcraft/ui/main-menu.rml @@ -3,6 +3,7 @@ MainMenu + AltCraft @@ -14,7 +15,7 @@ AC.ConnectToServer( document:GetElementById('hostname'):GetAttribute('value'), document:GetElementById('username'):GetAttribute('value'))">Connect - Options... + diff --git a/cwd/assets/altcraft/ui/mc-styles.rcss b/cwd/assets/altcraft/ui/mc-styles.rcss index e6dba35..3549180 100644 --- a/cwd/assets/altcraft/ui/mc-styles.rcss +++ b/cwd/assets/altcraft/ui/mc-styles.rcss @@ -39,8 +39,10 @@ body { .mc-button:hover { background-color: #7e86bc; + color: #cfd69d; } .mc-button:disabled { - background-color: #393939; -} \ No newline at end of file + background-color: #2b2b2b; + color: #848484; +} diff --git a/cwd/assets/altcraft/ui/options-styles.rcss b/cwd/assets/altcraft/ui/options-styles.rcss new file mode 100644 index 0000000..fbfc685 --- /dev/null +++ b/cwd/assets/altcraft/ui/options-styles.rcss @@ -0,0 +1,84 @@ +.body-options { + background-color: transparent; +} + +form { + width: 70%; + display: block; + margin: 5% auto; + background-color: #211710; +} + +.option { + display: block; + background-color: #0f0b07; + margin: 1vh; + padding: 0.5vh; + font-size: 4vh; + text-align: center; +} + +label { + +} + +span { + +} + +span:hover { + color: #cfd69d; +} + +input { + background-color: #2c2c2c; + height: 4vh; + margin-right: 1vh; + margin-left: 1vh; +} + +input.checkbox { + border-width: 1vh; + border-color: #2c2c2c; +} + +input.checkbox:checked { + background-color: #dcdadc; +} + +input.checkbox:hover { + border-color: #6a6b70; +} + +input.range:hover { + background-color: #6a6b70; +} + +input.range sliderbar { + width: 3vh; + background-color: #9c9c9c; +} + +input.range:hover sliderbar { + background-color: #e9e7e8; +} + +input.range sliderbar:active { + background-color: #cfd69d; +} + +input.range sliderarrowdec { + display: none; +} + +input.range sliderarrowinc { + display: none; +} + +#done { + display: block; + width: 45%; + height: 8%; + position: fixed; + margin: 5% auto auto; +} diff --git a/cwd/assets/altcraft/ui/options.rml b/cwd/assets/altcraft/ui/options.rml new file mode 100644 index 0000000..485dcba --- /dev/null +++ b/cwd/assets/altcraft/ui/options.rml @@ -0,0 +1,61 @@ + + + + + + Options + + +
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + +
diff --git a/cwd/assets/altcraft/ui/pause.rml b/cwd/assets/altcraft/ui/pause.rml index 2e366f8..5d652f9 100644 --- a/cwd/assets/altcraft/ui/pause.rml +++ b/cwd/assets/altcraft/ui/pause.rml @@ -2,6 +2,7 @@ + Paused @@ -9,7 +10,7 @@ Advancements Statistics - Options... + -- cgit v1.2.3