Skip to content

Commit

Permalink
Merge pull request #8 from VertexDezign/fs25
Browse files Browse the repository at this point in the history
Fs25
  • Loading branch information
Grisu118 authored Nov 16, 2024
2 parents 43fd87c + 953f154 commit 3e11d70
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 102 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Copyright (c) 2017 VertexDezign All rights reserved.
Copyright (c) 2017 Benjamin Leber All rights reserved.
Copyright (c) 2017-2024 VertexDezign All rights reserved.
Copyright (c) 2017-2024 Benjamin Leber All rights reserved.

This copyright does not impugn any trademarks or copyrights owned by Giants

Expand Down
27 changes: 20 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,46 @@
[![ModHub Download](https://img.shields.io/badge/%5BFS17%5D%20ModHub-2.0.1.0-green.svg?style=flat-square)](https://farming-simulator.com/mod.php?lang=de&country=ch&mod_id=51459&title=fs2017)
[![ModHub Download](https://img.shields.io/badge/%5BFS19%5D%20ModHub-1.1.1.0-green.svg?style=flat-square)](https://farming-simulator.com/mod.php?lang=de&country=ch&mod_id=117935&title=fs2019)
[![ModHub Download](https://img.shields.io/badge/%5BFS22%5D%20ModHub-1.0.0.0-blue.svg?style=flat-square)](https://farming-simulator.com/mod.php?mod_id=224748)
[![ModHub Download](https://img.shields.io/badge/%5BFS25%5D%20ModHub-1.0.0.0-green.svg?style=flat-square)](https://farming-simulator.com/mod.php?mod_id=303705)

A small script, which shows the actual time in the upper right corner.

## Features

* Show time on ui
* Customize in xml found in your mod dir after first start
* Customize in xml found in your mod dir after first start
* Position <position isDynamic="bool" x="[0,1]" y="[0,1]"/>
* isDynamic
* true: automatically position the text in the upper right corner considering his dimensions, ignore x and y
* true: automatically position the text below the ingame clock, ignore x and y
* false: Use x and y ignoring text dimension
* x, y: the position on the screen, see small illustration below for details
```
1,0 .. 1,1
0,0 .. 1,0
```
* Color and size of text <rendering color="black|white|r,g,b,a" fontSize="[0,1]"/>
* color: supports the following text values: black, white and all rgba colors seperated by comma in the range [0..1] example king blue "0.25,0.41,0.88,1"
* fontSize: the size of the font to display, a value in the range [0..1], default 0.015. UI Scale is used for calculating the font size to display
* color: supports the following text values: black, white and all rgba colors seperated by comma in the
range [0..1] example king blue "0.25,0.41,0.88,1"
* fontSize: the size of the font to display, a value in the range [0..1], default 0.015. UI Scale is used for
calculating the font size to display
* datetime format (placeholders like here in os.date -> https://www.lua.org/pil/22.1.html)
* Position and font size can now also be set with console commands, this makes it a lot easier to position the clock
at the wanted position
* vdRealClockPrintCurrentValues -> Shows the current values which are used
* vdRealClockSetPosition 0.9 0.9 -> Set the position, applies immediately
* vdRealClockSetFontSize 0.013 -> Set the font size, applies immediately
* vdRealClockResetToDefault -> Set the values back to their defaults
* vdRealClockSaveSettings -> Saves the changed values in the xml within the modSettings folder

## Credits

* Grisu118
* Slivicon (config file)

# LICENSE
Copyright (c) 2017-2022 VertexDezign All rights reserved.
Copyright (c) 2017-2022 Benjamin Leber All rights reserved.

Copyright (c) 2017-2024 VertexDezign All rights reserved.
Copyright (c) 2017-2024 Benjamin Leber All rights reserved.

This copyright does not impugn any trademarks or copyrights owned by Giants

Expand All @@ -46,7 +59,7 @@ The [Terms and Conditions of GIANTS Software GmbH](https://www.farming-simulator
An informal explanation of what this all means (this part is not legalese and can't be treated as such)

VertexDezign (we) wrote the Mod and we have the copyright on this Mod. That means the code is ours and we can
do with it what we want. It also means you can't just copy our code and use it for your own projects.
do with it what we want. It also means you can't just copy our code and use it for your own projects.
Only we can distribute the Mod to others. We allow you to make small changes for your own gameplay or with your friends.
But you are not allowed to publish these changes openly. When you make a contribution (translations, code changes) you
give that code to us. Otherwise we would not be able to publish this Mod anymore.
Expand Down
165 changes: 104 additions & 61 deletions RealClock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
-- v2.1 - 2019-01-14 - Move settings file to modSettings folder, do not draw when showTime is false
-- v2.1.1 - 2019-04-01 - Create modSettings folder
-- v3.0 - 2021-11-19 - FS22
-- v4.0 - 2024-11-07 - FS25
-- v4.1 - 2024-11-08 - Add console interface for positioning
-- v4.2 - 2024-11-09 - Move dynamic position into game info display below game clock
-- @descripion: Shows the real time clock in the upper right corner
-- @web: http://grisu118.ch or http://vertexdezign.net
-- Copyright (C) Grisu118, All Rights Reserved.
Expand All @@ -27,13 +30,13 @@ RealClock.d.rendering.fontSize = 0.015

local function protect(tbl)
return setmetatable(
{},
{
__index = tbl,
__newindex = function(t, key, value)
error("attempting to change constant " .. tostring(key) .. " to " .. tostring(value), 2)
end
}
{},
{
__index = tbl,
__newindex = function(t, key, value)
error("attempting to change constant " .. tostring(key) .. " to " .. tostring(value), 2)
end
}
)
end

Expand All @@ -56,31 +59,14 @@ function RealClock:loadMap(name)
self.timeFormat = RealClock.d.timeFormat

if g_dedicatedServerInfo == nil then
local xmlFile = g_modsDirectory .. "/" .. RealClock.configFileName
if not fileExists(xmlFile) then
local modSettingsDir = getUserProfileAppPath() .. "modSettings"
local newXmlFile = modSettingsDir .. "/" .. RealClock.configFileName
if not fileExists(newXmlFile) then
createFolder(modSettingsDir)

-- Special handling for wrongly named file
local wrongNamedFile = modSettingsDir .. RealClock.configFileName
if (fileExists(wrongNamedFile)) then
self:setValuesFromXML(wrongNamedFile)
self:writeCurrentConfig(newXmlFile)
self.debugger:warn("Config file created at correct place, you can safely delete the old file: " .. wrongNamedFile)
else
self:writeDefaultConfig(newXmlFile)
end
end
self:setValuesFromXML(newXmlFile)
else
self.debugger:warn(
"Loading configuration from mod folder, this is deprecated and will be removed in a furhter version. Move your " ..
RealClock.configFileName .. " in the modSettings folder."
)
self:setValuesFromXML(xmlFile)
local modSettingsDir = getUserProfileAppPath() .. "modSettings"
local newXmlFile = modSettingsDir .. "/" .. RealClock.configFileName
self.settingsFile = newXmlFile
if not fileExists(newXmlFile) then
createFolder(modSettingsDir)
self:writeDefaultConfig(newXmlFile)
end
self:setValuesFromXML(newXmlFile)
end
end

Expand All @@ -97,7 +83,7 @@ function RealClock:update(dt)
end

function RealClock:draw()
if g_dedicatedServerInfo ~= nil or not g_currentMission.hud.showTime then
if g_dedicatedServerInfo ~= nil or not g_currentMission.hud.gameInfoDisplay.isVisible or not g_currentMission.hud.isVisible then
return
end

Expand All @@ -109,10 +95,18 @@ function RealClock:draw()
local posX = self.position.x
local posY = self.position.y
if self.position.dynamic then
local width = getTextWidth(fontSize, date)
local height = getTextHeight(fontSize, date)
posX = 0.99 - width
posY = 1 - height
local gameClockFontSize = g_currentMission.hud.gameInfoDisplay.clockTextSize
-- make hour clock a little bit smaller than the original
fontSize = gameClockFontSize * 0.75
-- we are a little bit to far right, so we move it two characters to the left
local width = getTextWidth(gameClockFontSize, "09")
local gameClockHeight = getTextHeight(gameClockFontSize, "12:53")
local height = getTextHeight(gameClockFontSize, date)
posX = g_currentMission.hud.gameInfoDisplay.helpOffsetXClock - width
-- top of clock text
posY = g_currentMission.hud.gameInfoDisplay.y - g_currentMission.hud.gameInfoDisplay.clockTextOffsetY
-- move it below the game clock
posY = posY - gameClockHeight - 2 * (height / 3)
end
renderText(posX, posY, fontSize, date)
setTextColor(1, 1, 1, 1)
Expand All @@ -136,9 +130,9 @@ end

function RealClock:writeDefaultConfig(fileName)
self.debugger:info(
function()
return "Write default Config to " .. fileName
end
function()
return "Write default Config to " .. fileName
end
)
local xml = createXMLFile("RealClock", fileName, "RealClock")
setXMLBool(xml, "RealClock.position#isDynamic", RealClock.d.position.dynamic)
Expand All @@ -153,9 +147,9 @@ end

function RealClock:writeCurrentConfig(fileName)
self.debugger:info(
function()
return "Write current Config to " .. fileName
end
function()
return "Write current Config to " .. fileName
end
)
local xml = createXMLFile("RealClock", fileName, "RealClock")
setXMLBool(xml, "RealClock.position#isDynamic", self.position.dynamic)
Expand All @@ -170,31 +164,31 @@ end

function RealClock:setValuesFromXML(fileName)
self.debugger:info(
function()
return "Read from xml " .. fileName
end
function()
return "Read from xml " .. fileName
end
)
local xml = loadXMLFile("RealClock", fileName)
self.position.dynamic = Utils.getNoNil(getXMLBool(xml, "RealClock.position#isDynamic"), RealClock.d.position.dynamic)
self.debugger:debug(
function()
return "Position.dynamic: " .. tostring(self.position.dynamic)
end
function()
return "Position.dynamic: " .. tostring(self.position.dynamic)
end
)
local x = Utils.getNoNil(getXMLFloat(xml, "RealClock.position#x"), RealClock.d.position.x)
self.debugger:debug(
function()
return "Position.x: " .. tostring(x)
end
function()
return "Position.x: " .. tostring(x)
end
)
if (self:validateFloat(x, "x")) then
self.position.x = x
end
local y = Utils.getNoNil(getXMLFloat(xml, "RealClock.position#y"), RealClock.d.position.y)
self.debugger:debug(
function()
return "Position.y: " .. tostring(y)
end
function()
return "Position.y: " .. tostring(y)
end
)
if (self:validateFloat(y, "y")) then
self.position.y = y
Expand All @@ -218,10 +212,10 @@ function RealClock:setValuesFromXML(fileName)
end
else
self.debugger:warn(
function()
return "Invalid value for color, only 'white', 'black' or a custom color like '0,0.5,1,0.8' allowed, found " ..
color
end
function()
return "Invalid value for color, only 'white', 'black' or a custom color like '0,0.5,1,0.8' allowed, found " ..
color
end
)
self.rendering.color = RealClock.d.rendering.color
end
Expand All @@ -243,12 +237,61 @@ function RealClock:validateFloat(float, text)
return true
else
self.debugger:warn(
function()
return "Invalid value for " .. text .. ", must be a value between 0 and 1 including both, was " .. float
end
function()
return "Invalid value for " .. text .. ", must be a value between 0 and 1 including both, was " .. float
end
)
return false
end
end

-- console interface for easier positioning of the clock
function RealClock:printCurrentValues()
print("X: " .. self.position.x .. " Y: " .. self.position.y .. " FontSize: " .. self.rendering.fontSize)
end

function RealClock:updatePosition(x, y)
if x == nil or y == nil then
self.debugger:warn("Two parameters are required <x> <y>, at least one was missing")
return
end
if self:validateFloat(x, "X") and self:validateFloat(y, "X") then
self.position.dynamic = false
self.position.x = tonumber(x)
self.position.y = tonumber(y)
end
end

function RealClock:updateFontSize(fontSize)
if fontSize == nil then
self.debugger:warn("One parameter required <fontSize>")
return
end
if self:validateFloat(fontSize, "FontSize") then
self.position.dynamic = false
self.rendering.fontSize = tonumber(fontSize)
end
end

function RealClock:saveSettings()
if self.settingsFile ~= nil then
self:writeCurrentConfig(self.settingsFile)
else
self.debugger:warn("settings file is not set")
end
end

function RealClock:resetToDefault()
self.position.dynamic = RealClock.d.position.dynamic
self.position.x = RealClock.d.position.x
self.position.y = RealClock.d.position.y
self.rendering.fontSize = RealClock.d.rendering.fontSize
end

addModEventListener(RealClock)

addConsoleCommand("vdRealClockPrintCurrentValues", "Prints the current position and fontSize into console", "printCurrentValues", RealClock)
addConsoleCommand("vdRealClockSetPosition", "Set the position of the clock, requires two arguments x and y with a value from 0 < 1", "updatePosition", RealClock)
addConsoleCommand("vdRealClockSetFontSize", "Set the font size of the clock, requires one argument", "updateFontSize", RealClock)
addConsoleCommand("vdRealClockSaveSettings", "Saves the current values into the settings file", "saveSettings", RealClock)
addConsoleCommand("vdRealClockResetToDefault", "Set the changed values back to their defaults", "resetToDefault", RealClock)
15 changes: 0 additions & 15 deletions deploy.sh

This file was deleted.

Binary file added icon_clock.dds
Binary file not shown.
Loading

0 comments on commit 3e11d70

Please sign in to comment.