Skip to content

Commit

Permalink
Merge pull request #17 from Courseplay/Gui-Additions
Browse files Browse the repository at this point in the history
Fixed info text hud position saving
  • Loading branch information
Tensuko authored Dec 18, 2024
2 parents d96a659 + 6741c3d commit b914c54
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
10 changes: 4 additions & 6 deletions Courseplay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Courseplay:loadUserSettings()
self.globalSettings:loadFromXMLFile(xmlFile, self.xmlKey)
g_cpInGameMenu:loadFromXMLFile(xmlFile, self.xmlKey)
CpBaseHud.loadFromXmlFile(xmlFile, self.xmlKey)
CpHudInfoTexts.loadFromXmlFile(xmlFile, self.xmlKey)
self.infoTextsHud:loadFromXmlFile(xmlFile, self.xmlKey)
xmlFile:save()
xmlFile:delete()
else
Expand All @@ -53,11 +53,11 @@ function Courseplay:saveUserSettings()
if xmlFile then
self.globalSettings:saveUserSettingsToXmlFile(xmlFile, self.xmlKey)
CpBaseHud.saveToXmlFile(xmlFile, self.xmlKey)
CpHudInfoTexts.saveToXmlFile(xmlFile, self.xmlKey)
self.infoTextsHud:saveToXmlFile(xmlFile, self.xmlKey)
if self.currentVersion then
xmlFile:setValue(self.baseXmlKey.."#lastVersion", self.currentVersion)
xmlFile:setValue(self.baseXmlKey .. "#lastVersion", self.currentVersion)
end
g_cpInGameMenu:saveToXMLFile(xmlFile, g_Courseplay.xmlKey)
g_cpInGameMenu:saveToXMLFile(xmlFile, self.xmlKey)
xmlFile:save()
xmlFile:delete()
end
Expand Down Expand Up @@ -130,8 +130,6 @@ end

function Courseplay:setupGui()
CpInGameMenu.setupGui(self.courseStorage)


self.infoTextsHud = CpHudInfoTexts()

-- TODO_25
Expand Down
10 changes: 6 additions & 4 deletions scripts/gui/hud/CpBaseHud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ function CpBaseHud:init(vehicle)
self.baseHud = CpHudMoveableElement.new(background)
self.baseHud:setPosition(CpBaseHud.x, CpBaseHud.y)
self.baseHud:setDimension(self.width, self.height)
self.baseHud:setCallback("onMove", self, self.moveToPosition)
self.baseHud:setCallback("onMove", self, function (self, _, x, y)
CpBaseHud.x = x
CpBaseHud.y = y
end)

self.fieldworkLayout = self:addHudPage(CpFieldWorkHudPageElement, vehicle)

Expand Down Expand Up @@ -438,9 +441,8 @@ function CpBaseHud:addLineTextButtonWithIncrementalButtons(parent, line, textSiz
end


function CpBaseHud:moveToPosition(element, x, y)
CpBaseHud.x = x
CpBaseHud.y = y
function CpBaseHud:moveToPosition(x, y)
self.baseHud:moveTo(x, y)
end

function CpBaseHud:openClose(open)
Expand Down
59 changes: 26 additions & 33 deletions scripts/gui/hud/CpHudInfoTexts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,15 @@ CpHudInfoTexts.OFF_COLOR = {0.2, 0.2, 0.2, 0.9}
CpHudInfoTexts.SELECTED_COLOR = {0, 0.6, 0, 0.9}

CpHudInfoTexts.xmlKey = "HudInfoTexts"
function CpHudInfoTexts.registerXmlSchema(xmlSchema,baseKey)
xmlSchema:register(XMLValueType.FLOAT,baseKey..CpHudInfoTexts.xmlKey.."#posX","Hud position x.")
xmlSchema:register(XMLValueType.FLOAT,baseKey..CpHudInfoTexts.xmlKey.."#posY","Hud position y.")
function CpHudInfoTexts.registerXmlSchema(xmlSchema, baseKey)
xmlSchema:register(XMLValueType.FLOAT, baseKey .. CpHudInfoTexts.xmlKey .. "#posX", "Hud position x")
xmlSchema:register(XMLValueType.FLOAT, baseKey .. CpHudInfoTexts.xmlKey .. "#posY", "Hud position y")
end

function CpHudInfoTexts:init()
self.uiScale = g_gameSettings:getValue("uiScale")


if CpHudInfoTexts.savedPositions then
CpHudInfoTexts.x, CpHudInfoTexts.y = unpack(CpHudInfoTexts.savedPositions)
CpHudInfoTexts.savedPositions = nil
end
if CpHudInfoTexts.x == nil or CpHudInfoTexts.y == nil then
CpHudInfoTexts.x, CpHudInfoTexts.y = getNormalizedScreenValues(self.basePosition.x, self.basePosition.y)
end

self.x, self.y = getNormalizedScreenValues(self.basePosition.x, self.basePosition.y)

self.width, self.height = getNormalizedScreenValues(self.baseSize.x, self.baseSize.y)

Expand All @@ -63,9 +56,12 @@ function CpHudInfoTexts:init()
background:setAlignment(Overlay.ALIGN_VERTICAL_TOP, Overlay.ALIGN_HORIZONTAL_LEFT)
--- Base hud element.
self.baseHud = CpHudMoveableElement.new(background)
self.baseHud:setPosition(CpHudInfoTexts.x, CpHudInfoTexts.y)
self.baseHud:setPosition(self.x, self.y)
self.baseHud:setDimension(self.width, self.height)
self.baseHud:setCallback("onMove", self, self.moveToPosition)
self.baseHud:setCallback("onMove", self, function(self, _, x, y)
self.x = x
self.y = y
end)

local headerHeight = self.hMargin/2
local headerBackground = Overlay.new(g_baseUIFilename, 0, 0, self.width, headerHeight)
Expand All @@ -74,19 +70,19 @@ function CpHudInfoTexts:init()
headerBackground:setAlignment(Overlay.ALIGN_VERTICAL_TOP, Overlay.ALIGN_HORIZONTAL_LEFT)

local topElement = CpHudElement.new(headerBackground, self.baseHud)
topElement:setPosition(CpHudInfoTexts.x, CpHudInfoTexts.y)
topElement:setPosition(self.x, self.y)
topElement:setDimension(self.width, headerHeight)

local leftTopText = CpTextHudElement.new(self.baseHud, CpHudInfoTexts.x + self.wMargin, CpHudInfoTexts.y - headerHeight + self.hMargin/16, self.titleFontSize)
local leftTopText = CpTextHudElement.new(self.baseHud, self.x + self.wMargin, self.y - headerHeight + self.hMargin/16, self.titleFontSize)
leftTopText:setTextDetails("Courseplay")
local rightTopText = CpTextHudElement.new(self.baseHud, CpHudInfoTexts.x + self.width - self.wMargin, CpHudInfoTexts.y - headerHeight + self.hMargin/16, self.titleFontSize, RenderText.ALIGN_RIGHT)
local rightTopText = CpTextHudElement.new(self.baseHud, self.x + self.width - self.wMargin, self.y - headerHeight + self.hMargin/16, self.titleFontSize, RenderText.ALIGN_RIGHT)
rightTopText:setTextDetails(g_Courseplay.currentVersion)

local width, height = getNormalizedScreenValues(20, 20)

local x = CpHudInfoTexts.x + self.wMargin
local x = self.x + self.wMargin
local dx = x + self.wMargin + width
local y = CpHudInfoTexts.y - self.hMargin - headerHeight + self.lineHeight
local y = self.y - self.hMargin - headerHeight + self.lineHeight
self.infoTextsElements = {}
for i=1, self.maxLines do
y = y - self.lineHeight
Expand Down Expand Up @@ -118,9 +114,8 @@ function CpHudInfoTexts:mouseEvent(posX, posY, isDown, isUp, button)
end
end

function CpHudInfoTexts:moveToPosition(element, x, y)
CpHudInfoTexts.x = x
CpHudInfoTexts.y = y
function CpHudInfoTexts:moveToPosition(x, y)
self.baseHud:moveTo(x, y)
end

function CpHudInfoTexts:draw()
Expand Down Expand Up @@ -210,25 +205,23 @@ function CpHudInfoTexts:delete()
end

function CpHudInfoTexts:debug(str,...)
CpUtil.debugFormat(CpDebug.DBG_HUD,"Info text hud "..str,...)
CpUtil.debugFormat(CpDebug.DBG_HUD, "Info text hud " .. str, ...)
end

--- Saves hud position.
function CpHudInfoTexts.saveToXmlFile(xmlFile,baseKey)
if CpHudInfoTexts.x ~= nil and CpHudInfoTexts.y ~= nil then
xmlFile:setValue(baseKey..CpHudInfoTexts.xmlKey.."#posX",CpHudInfoTexts.x)
xmlFile:setValue(baseKey..CpHudInfoTexts.xmlKey.."#posY",CpHudInfoTexts.y)
function CpHudInfoTexts:saveToXmlFile(xmlFile, baseKey)
if self.x ~= nil and self.y ~= nil then
xmlFile:setValue(baseKey .. self.xmlKey .. "#posX", self.x)
xmlFile:setValue(baseKey .. self.xmlKey .. "#posY", self.y)
end
end

--- Loads hud position.
function CpHudInfoTexts.loadFromXmlFile(xmlFile,baseKey)
local posX = xmlFile:getValue(baseKey..CpHudInfoTexts.xmlKey.."#posX")
local posY = xmlFile:getValue(baseKey..CpHudInfoTexts.xmlKey.."#posY")
function CpHudInfoTexts:loadFromXmlFile(xmlFile, baseKey)
local posX = xmlFile:getValue(baseKey .. self.xmlKey .. "#posX")
local posY = xmlFile:getValue(baseKey .. self.xmlKey .. "#posY")
if posX ~= nil and posY ~= nil then
CpHudInfoTexts.savedPositions = {
posX, posY
}
self:moveToPosition(posX, posY)
end
end

Expand Down
1 change: 1 addition & 0 deletions scripts/gui/pages/CpGlobalSettingsFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ function CpGlobalSettingsFrame:updateSubCategoryPages(state)
self.subCategoryPages[state]:setVisible(true)
self.subCategoryTabs[state]:setSelected(true)
local layout = self.subCategoryPages[state]:getDescendantByName("layout")
CpSettingsUtil.updateGuiElementsBoundToSettings(layout)
self.settingsSlider:setDataElement(layout)
end

0 comments on commit b914c54

Please sign in to comment.