summaryrefslogtreecommitdiffstats
path: root/cwd/assets/altcraft/scripts/ui.lua
blob: 392ad2716d05e8028dd68bcb5f81ea3766ea6b84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 ConnectToServer(doc)
	AC.Settings.Write('hostname',doc:GetElementById('hostname'):GetAttribute('value'))
    AC.Settings.Write('username',doc:GetElementById('username'):GetAttribute('value'))
    AC.Settings.Save()
    AC.ConnectToServer(
		doc:GetElementById('hostname'):GetAttribute('value'),
		doc:GetElementById('username'):GetAttribute('value'))
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

local lastFps = {}

local function UpdateFps(newFps)
	lastFps[#lastFps + 1] = newFps
	if #lastFps >= 100 then
		table.remove(lastFps, 1)
	end
	local smoothFps = 0
	for i,v in ipairs(lastFps) do
		smoothFps = smoothFps + v
	end
	smoothFps = smoothFps / #lastFps
	return smoothFps
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 time = AC.GetTime()
		local rawFps = 1.0 / time:GetRealDeltaS()
		local smoothFps = UpdateFps(rawFps)
		doc:GetElementById('dbg-fps').inner_rml = string.format("%.1f", smoothFps)

		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 wrld = AC.GetGameState():GetWorld()
		local selection = AC.GetGameState():GetSelectionStatus()
		if selection.isBlockSelected then
			bid = wrld:GetBlockId(selection.selectedBlock)
			doc:GetElementById('dbg-select-pos').inner_rml = tostring(selection.selectedBlock)
			doc:GetElementById('dbg-select-bid').inner_rml = string.format("%d:%d", bid.id, bid.state)
		else
			doc:GetElementById('dbg-select-pos').inner_rml = ""
			doc:GetElementById('dbg-select-bid').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