Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs) qmlfrontend
authorunc0rr
Sat, 25 Oct 2014 00:31:10 +0400
branchqmlfrontend
changeset 10444 47a6231f1fc1
parent 10442 c58db813240b
child 10446 7ae44f42a689
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
hedgewars/hwLibrary.pas
hedgewars/uFLGameConfig.pas
hedgewars/uFLTypes.pas
qmlFrontend/flib.h
qmlFrontend/hwengine.cpp
qmlFrontend/hwengine.h
qmlFrontend/qml/qmlFrontend/GameConfig.qml
--- a/hedgewars/hwLibrary.pas	Mon Oct 20 22:55:12 2014 +0400
+++ b/hedgewars/hwLibrary.pas	Sat Oct 25 00:31:10 2014 +0400
@@ -157,6 +157,8 @@
     freeThemesList,
     getThemeIcon,
     getTeamsList,
+    tryAddTeam,
+    tryRemoveTeam,
     LoadLocaleWrapper,
     HW_versionInfo,
     HW_versionString,
--- a/hedgewars/uFLGameConfig.pas	Mon Oct 20 22:55:12 2014 +0400
+++ b/hedgewars/uFLGameConfig.pas	Sat Oct 25 00:31:10 2014 +0400
@@ -11,6 +11,9 @@
 procedure setSeed(seed: PChar); cdecl;
 function  getSeed: PChar; cdecl;
 
+procedure tryAddTeam(teamName: PChar);
+procedure tryRemoveTeam(teamName: PChar);
+
 implementation
 uses uFLIPC, hwengine, uFLUtils, uFLTeams;
 
@@ -153,4 +156,26 @@
     registerIPCCallback(nil, @engineMessageCallback)
 end;
 
+
+procedure tryAddTeam(teamName: PChar);
+var msg: ansistring;
+begin
+    msg:= '0' + #10 + teamName;
+
+    guiCallbackFunction(guiCallbackPointer, mtAddPlayingTeam, @msg[1], length(msg));
+
+    msg:= teamName;
+
+    guiCallbackFunction(guiCallbackPointer, mtRemoveTeam, @msg[1], length(msg))
+end;
+
+procedure tryRemoveTeam(teamName: PChar);
+var msg: ansistring;
+begin
+    msg:= teamName;
+
+    guiCallbackFunction(guiCallbackPointer, mtRemovePlayingTeam, @msg[1], length(msg));
+    guiCallbackFunction(guiCallbackPointer, mtAddTeam, @msg[1], length(msg))
+end;
+
 end.
--- a/hedgewars/uFLTypes.pas	Mon Oct 20 22:55:12 2014 +0400
+++ b/hedgewars/uFLTypes.pas	Sat Oct 25 00:31:10 2014 +0400
@@ -2,7 +2,7 @@
 interface
 
 type
-    TMessageType = (mtPreview);
+    TMessageType = (mtPreview, mtAddPlayingTeam, mtRemovePlayingTeam, mtAddTeam, mtRemoveTeam);
 
     TIPCMessage = record
                    str: shortstring;
--- a/qmlFrontend/flib.h	Mon Oct 20 22:55:12 2014 +0400
+++ b/qmlFrontend/flib.h	Sat Oct 25 00:31:10 2014 +0400
@@ -9,6 +9,10 @@
 
 enum MessageType {
     MSG_PREVIEW
+    , MSG_ADDPLAYINGTEAM
+    , MSG_REMOVEPLAYINGTEAM
+    , MSG_ADDTEAM
+    , MSG_REMOVETEAM
 };
 
 typedef union string255_
@@ -36,6 +40,8 @@
 typedef uint32_t getThemeIcon_t(char * theme, char * buffer, uint32_t size);
 
 typedef char **getTeamsList_t();
+typedef void tryAddTeam_t(const char * seed);
+typedef void tryRemoveTeam_t(const char * seed);
 
 #ifdef __cplusplus
 }
--- a/qmlFrontend/hwengine.cpp	Mon Oct 20 22:55:12 2014 +0400
+++ b/qmlFrontend/hwengine.cpp	Sat Oct 25 00:31:10 2014 +0400
@@ -21,6 +21,8 @@
     freeThemesList_t *flibFreeThemesList;
     getThemeIcon_t *flibGetThemeIcon;
     getTeamsList_t *flibGetTeamsList;
+    tryAddTeam_t * flibTryAddTeam;
+    tryRemoveTeam_t * flibTryRemoveTeam;
 }
 
 Q_DECLARE_METATYPE(MessageType);
@@ -50,6 +52,8 @@
     flibGetThemeIcon = (getThemeIcon_t*) hwlib.resolve("getThemeIcon");
 
     flibGetTeamsList = (getTeamsList_t*) hwlib.resolve("getTeamsList");
+    flibTryAddTeam = (tryAddTeam_t*) hwlib.resolve("tryAddTeam");
+    flibTryRemoveTeam = (tryRemoveTeam_t*) hwlib.resolve("tryRemoveTeam");
 
     flibInit("/usr/home/unC0Rr/Sources/Hedgewars/Hedgewars-GC/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars");
     flibRegisterGUIMessagesCallback(this, &guiMessagesCallback);
@@ -95,7 +99,7 @@
 void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len)
 {
     HWEngine * obj = (HWEngine *)context;
-    QByteArray b = QByteArray::fromRawData(msg, len);
+    QByteArray b = QByteArray(msg, len);
 
     qDebug() << "FLIPC in" << b.size() << b;
 
@@ -106,12 +110,30 @@
 {
     switch(mt)
     {
-    case MSG_PREVIEW:
+    case MSG_PREVIEW: {
         PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
         preview->setPixmap(msg);
         emit previewImageChanged();
         break;
     }
+    case MSG_ADDPLAYINGTEAM: {
+        QStringList l = QString::fromUtf8(msg).split('\n');
+        emit playingTeamAdded(l[1], l[0].toInt(), true);
+        break;
+    }
+    case MSG_REMOVEPLAYINGTEAM: {
+        emit playingTeamRemoved(msg);
+        break;
+    }
+    case MSG_ADDTEAM: {
+        emit localTeamAdded(msg, 0);
+        break;
+    }
+    case MSG_REMOVETEAM: {
+        emit localTeamRemoved(msg);
+        break;
+    }
+    }
 }
 
 QString HWEngine::currentSeed()
@@ -143,3 +165,13 @@
         emit localTeamAdded(team, 0);
     }
 }
+
+void HWEngine::tryAddTeam(const QString &teamName)
+{
+    flibTryAddTeam(teamName.toUtf8().constData());
+}
+
+void HWEngine::tryRemoveTeam(const QString &teamName)
+{
+    flibTryRemoveTeam(teamName.toUtf8().constData());
+}
--- a/qmlFrontend/hwengine.h	Mon Oct 20 22:55:12 2014 +0400
+++ b/qmlFrontend/hwengine.h	Sat Oct 25 00:31:10 2014 +0400
@@ -22,11 +22,18 @@
     Q_INVOKABLE void runQuickGame();
     Q_INVOKABLE QString currentSeed();
     Q_INVOKABLE void getTeamsList();
-    
+
+    Q_INVOKABLE void tryAddTeam(const QString & teamName);
+    Q_INVOKABLE void tryRemoveTeam(const QString & teamName);
+
 signals:
     void previewImageChanged();
     void localTeamAdded(const QString & teamName, int aiLevel);
-    
+    void localTeamRemoved(const QString & teamName);
+
+    void playingTeamAdded(const QString & teamName, int aiLevel, bool isLocal);
+    void playingTeamRemoved(const QString & teamName);
+
 public slots:
 
 private:
--- a/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Mon Oct 20 22:55:12 2014 +0400
+++ b/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Sat Oct 25 00:31:10 2014 +0400
@@ -83,7 +83,7 @@
         clip: true
 
         model: ListModel {
-            id: localTeamsModel
+            id: playingTeamsModel
         }
 
         delegate: Rectangle {
@@ -97,11 +97,68 @@
             Row {
                 Text { text: name }
             }
+
+            MouseArea {
+                 z: 1
+                 anchors.fill: parent
+                 onClicked: HWEngine.tryRemoveTeam(name)
+            }
+        }
+
+        Connections {
+            target: HWEngine
+            onPlayingTeamAdded: playingTeamsModel.append({"aiLevel": aiLevel, "name": teamName, "local": isLocal})
+            onPlayingTeamRemoved: {
+                var i = playingTeamsModel.count - 1;
+                while ((i >= 0) && (playingTeamsModel.get(i).name !== teamName)) --i
+
+                if(i >= 0) playingTeamsModel.remove(i, 1)
+            }
+        }
+    }
+
+    ListView {
+        id: localTeamsList
+        x: 440
+        y: 224
+        width: 100
+        height: 192
+        highlight: Rectangle { color: "#eaea00"; radius: 4 }
+        focus: true
+        clip: true
+
+        model: ListModel {
+            id: localTeamsModel
+        }
+
+        delegate: Rectangle {
+            id: localTeamDelegate
+            height: 24
+            width: parent.width
+            radius: 8
+            border.width: 2
+            border.color: "#eaea00"
+
+            Row {
+                Text { text: name }
+            }
+
+            MouseArea {
+                 z: 1
+                 anchors.fill: parent
+                 onClicked: HWEngine.tryAddTeam(name)
+            }
         }
 
         Connections {
             target: HWEngine
             onLocalTeamAdded: localTeamsModel.append({"aiLevel": aiLevel, "name": teamName})
+            onLocalTeamRemoved: {
+                var i = localTeamsModel.count - 1;
+                while ((i >= 0) && (localTeamsModel.get(i).name !== teamName)) --i
+
+                if(i >= 0) localTeamsModel.remove(i, 1)
+            }
         }
     }