瀏覽代碼

use colors and custom text for each recording / websocket connection state

Noah Vogt 21 小時之前
父節點
當前提交
9d6e8456e2
共有 3 個文件被更改,包括 38 次插入8 次删除
  1. 2 0
      README.md
  2. 36 8
      cd-rec-status.cpp
  3. 二進制
      recording_active.jpg

+ 2 - 0
README.md

@@ -2,6 +2,8 @@
 
 An [OBS](https://github.com/obsproject/obs-studio) plugin to display the status of an ongoing cd recording as a dock.
 
+![](recording_active.jpg)
+
 The [offical obs plugin template](https://github.com/obsproject/obs-plugintemplate) was used to create this plugin.
 
 ## How it works

+ 36 - 8
cd-rec-status.cpp

@@ -32,13 +32,16 @@ public:
         delete socket;
     }
 
+
 private slots:
     void onConnected() {
         statusLabel->setText("Connected to status server");
+        updateBackgroundColor(Qt::gray);  // assume no recording yet
     }
 
     void onDisconnected() {
         statusLabel->setText("Disconnected from server");
+        updateBackgroundColor(Qt::red);
     }
 
     void onMessageReceived(const QString &message) {
@@ -46,20 +49,45 @@ private slots:
         QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &parseError);
         if (parseError.error != QJsonParseError::NoError || !doc.isObject()) {
             statusLabel->setText("Invalid status message");
+            updateBackgroundColor(Qt::red);
             return;
         }
 
         QJsonObject obj = doc.object();
-        QString statusText = QString("Recording: %1\nCD: %2\nTrack: %3\nElapsed CD Time: %4\nElapsed Track Time: %5")
-                                .arg(obj["recording"].toBool() ? "Yes" : "No")
-                                .arg(obj["cd"].toInt())
-                                .arg(obj["track"].toInt())
-                                .arg(obj["cd_time"].toString())
-                                .arg(obj["track_time"].toString());
-
-        statusLabel->setText(statusText);
+        bool isRecording = obj["recording"].toBool();
+
+	QString statusText;
+	if (isRecording) {
+		statusText =
+			QString("Recording: active\nCD: %2\nTrack: %3\nElapsed CD Time: %4\nElapsed Track Time: %5")
+				.arg(obj["cd"].toInt())
+				.arg(obj["track"].toInt())
+				.arg(obj["cd_time"].toString())
+				.arg(obj["track_time"].toString());
+	} else {
+		statusText = QString("Recording is currently not active.");
+	}
+
+	statusLabel->setText(statusText);
+        updateBackgroundColor(isRecording ? QColor("#228B22") : Qt::gray);
     }
 
+private:
+    void updateBackgroundColor(const QColor &bgColor) {
+    this->setStyleSheet(QString(
+        "background-color: %1;"
+        "color: %2;"
+    ).arg(bgColor.name())
+     .arg(isColorDark(bgColor) ? "white" : "black"));
+}
+
+bool isColorDark(const QColor &color) {
+    // Perceived brightness formula
+    int brightness = (color.red() * 299 + color.green() * 587 + color.blue() * 114) / 1000;
+    return brightness < 128;
+}
+
+
 private:
     QLabel *statusLabel;
     QWebSocket *socket;

二進制
recording_active.jpg