-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tank gauge via clip instead of opacity mask
The opacity mask shader effect sometimes didn't render properly on device. This commit adds a new clip-based tank gauge instead.
- Loading branch information
Showing
4 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
** Copyright (C) 2024 Victron Energy B.V. | ||
** See LICENSE.txt for license information. | ||
*/ | ||
|
||
import QtQuick | ||
import QtQuick.Effects as Effects | ||
import Victron.VenusOS | ||
import Victron.Gauges | ||
|
||
Item { | ||
id: root | ||
|
||
property int valueType: VenusOS.Gauges_ValueType_NeutralPercentage | ||
readonly property int valueStatus: Gauges.getValueStatus(_value * 100, valueType) | ||
property color foregroundColor: Theme.statusColorValue(valueStatus) | ||
property color backgroundColor: Theme.statusColorValue(valueStatus, true) | ||
property color surfaceColor: Theme.color_levelsPage_gauge_separatorBarColor | ||
property real value: 0.0 | ||
property int orientation: Qt.Vertical | ||
property real radius: root.orientation === Qt.Vertical ? width / 2 : height / 2 | ||
property bool animationEnabled | ||
|
||
width: orientation === Qt.Vertical ? Theme.geometry_barGauge_vertical_width_large : parent.width | ||
height: orientation === Qt.Vertical ? parent.height : Theme.geometry_barGauge_horizontal_height | ||
clip: true | ||
|
||
readonly property real _value: isNaN(value) ? 0 : Math.min(1.0, Math.max(0, value)) | ||
|
||
// Only update the nextPos when the width/height have been initialized. | ||
readonly property real _nextPos: (width !== Infinity && height !== Infinity) | ||
? orientation === Qt.Vertical | ||
? height - (height * _value) // slide in from bottom to top | ||
: -width + (width * _value) // slide in from left to right | ||
: 0 | ||
|
||
on_NextPosChanged: if (visible) _updateGauge() | ||
onVisibleChanged: if (visible) _updateGauge() | ||
|
||
function _updateGauge() { | ||
if (animationEnabled) { | ||
const animator = orientation === Qt.Vertical ? yAnimator : xAnimator | ||
const currValue = orientation === Qt.Vertical ? fgRect.y : fgRect.x | ||
if (!animator.running && currValue !== _nextPos) { | ||
animator.from = currValue | ||
animator.to = _nextPos | ||
animator.start() | ||
} | ||
} else { | ||
if (orientation === Qt.Vertical) { | ||
fgRect.y = _nextPos | ||
} else { | ||
fgRect.x = _nextPos | ||
} | ||
} | ||
} | ||
|
||
Rectangle { | ||
id: bgRect | ||
anchors.fill: parent | ||
color: root.backgroundColor | ||
z: 1 // drawn below fg and below border | ||
} | ||
|
||
Rectangle { | ||
id: fgRect | ||
|
||
width: parent.width | ||
height: parent.height | ||
color: foregroundColor | ||
z: 2 // drawn above bg and below border | ||
|
||
// Use animators instead of a behavior on x/y. Otherwise, there can be a "jump" when | ||
// receiving two value updates in close succession. | ||
XAnimator { | ||
id: xAnimator | ||
target: fgRect | ||
easing.type: Easing.InOutQuad | ||
duration: Theme.animation_briefPage_sidePanel_sliderValueChange_duration | ||
onRunningChanged: if (!running) Qt.callLater(_updateGauge) // if _nextPos changed during previous animation | ||
} | ||
YAnimator { | ||
id: yAnimator | ||
target: fgRect | ||
easing.type: Easing.InOutQuad | ||
duration: Theme.animation_briefPage_sidePanel_sliderValueChange_duration | ||
onRunningChanged: if (!running) Qt.callLater(_updateGauge) // if _nextPos changed during previous animation | ||
} | ||
} | ||
|
||
Rectangle { | ||
id: borderRect | ||
color: "transparent" | ||
radius: root.radius + border.width // "exterior" radius = "internal" radius + border.width | ||
width: parent.width + 2*border.width | ||
height: parent.height + 2*border.width | ||
x: -border.width | ||
y: -border.width | ||
z: 5 // drawn above everything else. | ||
// wide enough to perfectly cover the "missing" pixels due to interior rounding. | ||
border.width: Math.ceil(((Math.SQRT2*(2*root.radius)) - (2*root.radius))/2) | ||
border.color: root.surfaceColor | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters