From 1526b0dce7e09855ddeeed0824abc1afbfab2d45 Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Sat, 5 Feb 2022 17:27:53 +0100
Subject: [PATCH] a bit more configurability for the decoration bar

---
 src/include/unittype.h       |  5 ++++-
 src/unit/script_unittype.cpp |  6 ++++++
 src/unit/unit_draw.cpp       | 11 +++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/include/unittype.h b/src/include/unittype.h
index 4acdda3ce..31f4968cc 100644
--- a/src/include/unittype.h
+++ b/src/include/unittype.h
@@ -277,7 +277,7 @@ public:
 class CDecoVarBar : public CDecoVar
 {
 public:
-	/// function to draw the decorations.
+	CDecoVarBar() : MinValue(0), MaxValue(100), Invert(false) {};
 	virtual void Draw(int x, int y, const CUnitType &type, const CVariable &var) const;
 
 	bool IsVertical;            /// if true, vertical bar, else horizontal.
@@ -285,6 +285,9 @@ public:
 	int Height;                 /// Height of the bar.
 	int Width;                  /// Width of the bar.
 	bool ShowFullBackground;    /// if true, show background like value equal to max.
+	bool Invert;                /// if true, invert length
+	int MinValue;               /// show only above percent
+	int MaxValue;               /// show only below percent
 	char BorderSize;            /// Size of the border, 0 for no border.
 	// FIXME color depend of percent (red, Orange, Yellow, Green...)
 	IntColor Color;             /// Color of bar.
diff --git a/src/unit/script_unittype.cpp b/src/unit/script_unittype.cpp
index 00d391319..45f03684c 100644
--- a/src/unit/script_unittype.cpp
+++ b/src/unit/script_unittype.cpp
@@ -2032,6 +2032,12 @@ static int CclDefineDecorations(lua_State *l)
 							decovarbar->Height = LuaToNumber(l, -1);
 						} else if (!strcmp(key, "Width")) {
 							decovarbar->Width = LuaToNumber(l, -1);
+						} else if (!strcmp(key, "MinValue")) {
+							decovarbar->MinValue = LuaToNumber(l, -1);
+						} else if (!strcmp(key, "MaxValue")) {
+							decovarbar->MaxValue = LuaToNumber(l, -1);
+						} else if (!strcmp(key, "Invert")) {
+							decovarbar->Invert = LuaToBoolean(l, -1);
 						} else if (!strcmp(key, "Orientation")) {
 							key = LuaToString(l, -1);
 							if (!strcmp(key, "horizontal")) {
diff --git a/src/unit/unit_draw.cpp b/src/unit/unit_draw.cpp
index 0363f7207..a72fa0f1a 100644
--- a/src/unit/unit_draw.cpp
+++ b/src/unit/unit_draw.cpp
@@ -391,6 +391,11 @@ void CDecoVarBar::Draw(int x, int y,
 					   const CUnitType &type, const CVariable &var) const
 {
 	Assert(var.Max);
+	
+	int percentage = var.Value * 100 / var.Max;
+	if (MinValue > percentage || MaxValue < percentage) {
+		return;
+	}
 
 	int height = this->Height;
 	if (height == 0) { // Default value
@@ -405,8 +410,14 @@ void CDecoVarBar::Draw(int x, int y,
 	if (this->IsVertical)  { // Vertical
 		w = width;
 		h = var.Value * height / var.Max;
+		if (Invert) {
+			h = height - h;
+		}
 	} else {
 		w = var.Value * width / var.Max;
+		if (Invert) {
+			w = width - w;
+		}
 		h = height;
 	}
 	if (this->IsCenteredInX) {