Themes model, also add some files I forgot to add previously qmlfrontend
authorunc0rr
Tue, 30 Sep 2014 00:54:04 +0400
branchqmlfrontend
changeset 10434 1614b13ad35e
parent 10432 b0abef0ee78c
child 10436 084e046f6bd5
Themes model, also add some files I forgot to add previously
hedgewars/CMakeLists.txt
hedgewars/hwLibrary.pas
hedgewars/uFLData.pas
hedgewars/uFLTeams.pas
hedgewars/uFLUtils.pas
hedgewars/uInputHandler.pas
hedgewars/uPhysFSLayer.pas
hedgewars/uTypes.pas
hedgewars/uVariables.pas
qmlFrontend/flib.h
qmlFrontend/hwengine.cpp
qmlFrontend/hwengine.h
qmlFrontend/qml/qmlFrontend/GameConfig.qml
--- a/hedgewars/CMakeLists.txt	Sun Sep 28 21:33:14 2014 +0400
+++ b/hedgewars/CMakeLists.txt	Tue Sep 30 00:54:04 2014 +0400
@@ -105,6 +105,7 @@
     uFLGameConfig.pas
     uFLTeams.pas
     uFLUtils.pas
+    uFLData.pas
 
     #these interact with everything, so compile last
     uScript.pas
--- a/hedgewars/hwLibrary.pas	Sun Sep 28 21:33:14 2014 +0400
+++ b/hedgewars/hwLibrary.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -42,6 +42,7 @@
     , uFLGameConfig
     , uFLIPC
     , uPhysFSLayer
+    , uFLData
     ;
 
 {$INCLUDE "config.inc"}
@@ -151,6 +152,8 @@
     flibFree,
     setSeed,
     getSeed,
+    getThemesList,
+    freeThemesList,
     LoadLocaleWrapper,
     HW_versionInfo,
     HW_versionString,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uFLData.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -0,0 +1,20 @@
+unit uFLData;
+interface
+
+function getThemesList: PPChar; cdecl;
+procedure freeThemesList(list: PPChar); cdecl;
+
+implementation
+uses uPhysFSLayer;
+
+function getThemesList: PPChar; cdecl;
+begin
+    getThemesList:= pfsEnumerateFiles('Themes')
+end;
+
+procedure freeThemesList(list: PPChar); cdecl;
+begin
+    pfsFreeList(list)
+end;
+
+end.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uFLTeams.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -0,0 +1,47 @@
+unit uFLTeams;
+interface
+uses uFLTypes;
+
+function createRandomTeam: TTeam;
+procedure sendTeamConfig(var team: TTeam);
+
+
+implementation
+uses uFLUtils, uFLIPC;
+
+function createRandomTeam: TTeam;
+var t: TTeam;
+    i: Longword;
+begin
+    with t do
+    begin
+        teamName:= 'team' + inttostr(random(100));
+
+        for i:= 0 to 7 do
+            with hedgehogs[i] do
+            begin
+                name:= 'hedgehog ' + inttostr(i);
+                hat:= 'NoHat'
+            end;
+
+        botLevel:= 0;
+        hogsNumber:= 4
+    end;
+    createRandomTeam:= t
+end;
+
+procedure sendTeamConfig(var team: TTeam);
+var i: Longword;
+begin
+    with team do
+    begin
+        ipcToEngine('eaddteam <hash> ' + color + ' ' + teamName);
+        for i:= 0 to Pred(hogsNumber) do
+        begin
+            ipcToEngine('eaddhh ' + inttostr(botLevel) + ' 100 ' + hedgehogs[i].name);
+            ipcToEngine('ehat ' + hedgehogs[i].hat);
+        end;
+    end
+end;
+
+end.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uFLUtils.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -0,0 +1,28 @@
+unit uFLUtils;
+interface
+
+function str2PChar(const s: shortstring): PChar;
+function intToStr(n: LongInt): shortstring;
+
+implementation
+
+var
+    str2PCharBuffer: array[0..255] of char;
+
+function str2PChar(const s: shortstring): PChar;
+var i: Integer;
+begin
+   for i:= 1 to Length(s) do
+      begin
+      str2PCharBuffer[i - 1] := s[i];
+      end;
+   str2PCharBuffer[Length(s)]:= #0;
+   str2PChar:= @(str2PCharBuffer[0]);
+end;
+
+function intToStr(n: LongInt): shortstring;
+begin
+    str(n, intToStr)
+end;
+
+end.
--- a/hedgewars/uInputHandler.pas	Sun Sep 28 21:33:14 2014 +0400
+++ b/hedgewars/uInputHandler.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -331,7 +331,7 @@
 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+char(i+48);
 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
 
-loadBinds('dbind', cPathz[ptData] + '/settings.ini');
+loadBinds('dbind', cPathz[ptConfig] + '/settings.ini');
 end;
 
 procedure SetBinds(var binds: TBinds);
@@ -472,7 +472,7 @@
     b: byte;
 begin
     if cOnlyStats then exit;
-    
+
     AddFileLog('[BINDS] Loading binds from: ' + s);
 
     l:= '';
--- a/hedgewars/uPhysFSLayer.pas	Sun Sep 28 21:33:14 2014 +0400
+++ b/hedgewars/uPhysFSLayer.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -28,6 +28,8 @@
 procedure pfsReadLnA(f: PFSFile; var s: ansistring);
 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
 function pfsEOF(f: PFSFile): boolean;
+function pfsEnumerateFiles(dir: shortstring): PPChar;
+procedure pfsFreeList(list: PPChar);
 
 function pfsExists(fname: shortstring): boolean;
 
@@ -51,6 +53,8 @@
 function PHYSFS_close(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_exists(fname: PChar): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_getLastError(): PChar; cdecl; external PhysfsLibName;
+function PHYSFS_enumerateFiles(dir: PChar): PPChar; cdecl; external PhysfsLibName;
+procedure PHYSFS_freeList(list: PPChar); cdecl; external PhysfsLibName;
 {$ELSE}
 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64;
 begin
@@ -88,6 +92,15 @@
     exit(PHYSFS_exists(Str2PChar(fname)))
 end;
 
+function pfsEnumerateFiles(dir: shortstring): PPChar;
+begin
+    exit(PHYSFS_enumerateFiles(Str2PChar(dir)))
+end;
+
+procedure pfsFreeList(list: PPChar);
+begin
+    PHYSFS_freeList(list)
+end;
 
 procedure pfsReadLn(f: PFSFile; var s: shortstring);
 var c: char;
@@ -169,12 +182,10 @@
 
     pfsMountAtRoot(localPrefix);
     pfsMountAtRoot(userPrefix + ansistring('/Data'));
+    pfsMount(userPrefix, PChar('/Config'));
 
     hedgewarsMountPackages;
 
-    // need access to teams and frontend configs (for bindings)
-    pfsMountAtRoot(userPrefix);
-
     {$IFNDEF PAS2C}
     if cTestLua then
         begin
--- a/hedgewars/uTypes.pas	Sun Sep 28 21:33:14 2014 +0400
+++ b/hedgewars/uTypes.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -45,7 +45,7 @@
     TPathType = (ptNone, ptData, ptGraphics, ptThemes, ptCurrTheme, ptTeams, ptMaps,
             ptMapCurrent, ptDemos, ptSounds, ptGraves, ptFonts, ptForts, ptLocale,
             ptAmmoMenu, ptHedgehog, ptVoices, ptHats, ptFlags, ptMissionMaps,
-            ptSuddenDeath, ptButtons, ptShaders);
+            ptSuddenDeath, ptButtons, ptShaders, ptConfig);
 
     // Available sprites for displaying stuff
     TSprite = (sprWater, sprCloud, sprBomb, sprBigDigit, sprFrame,
--- a/hedgewars/uVariables.pas	Sun Sep 28 21:33:14 2014 +0400
+++ b/hedgewars/uVariables.pas	Tue Sep 30 00:54:04 2014 +0400
@@ -244,11 +244,11 @@
     // these consts are here because they would cause circular dependencies in uConsts/uTypes
     cPathz: array[TPathType] of shortstring = (
         '',                              // ptNone
-        '//',                            // ptData
+        '/',                             // ptData
         '/Graphics',                     // ptGraphics
         '/Themes',                       // ptThemes
         '/Themes/Bamboo',                // ptCurrTheme
-        '/Teams',                        // ptTeams
+        '/Config/Teams',                 // ptTeams
         '/Maps',                         // ptMaps
         '',                              // ptMapCurrent
         '/Demos',                        // ptDemos
@@ -265,7 +265,8 @@
         '/Missions/Maps',                // ptMissionMaps
         '/Graphics/SuddenDeath',         // ptSuddenDeath
         '/Graphics/Buttons',             // ptButton
-        '/Shaders'                       // ptShaders
+        '/Shaders',                      // ptShaders
+        '/Config'                        // ptConfig
     );
 
 var
--- a/qmlFrontend/flib.h	Sun Sep 28 21:33:14 2014 +0400
+++ b/qmlFrontend/flib.h	Tue Sep 30 00:54:04 2014 +0400
@@ -31,6 +31,9 @@
 typedef void flibInit_t(const char * localPrefix, const char * userPrefix);
 typedef void flibFree_t();
 
+typedef char **getThemesList_t();
+typedef void freeThemesList_t(char **list);
+
 #ifdef __cplusplus
 }
 #endif
--- a/qmlFrontend/hwengine.cpp	Sun Sep 28 21:33:14 2014 +0400
+++ b/qmlFrontend/hwengine.cpp	Tue Sep 30 00:54:04 2014 +0400
@@ -16,6 +16,8 @@
     runQuickGame_t *flibRunQuickGame;
     flibInit_t *flibInit;
     flibFree_t *flibFree;
+    getThemesList_t *flibGetThemesList;
+    freeThemesList_t *flibFreeThemesList;
 }
 
 Q_DECLARE_METATYPE(MessageType);
@@ -40,8 +42,13 @@
     flibInit = (flibInit_t*) hwlib.resolve("flibInit");
     flibFree = (flibFree_t*) hwlib.resolve("flibFree");
 
-    flibInit("/usr/home/unC0Rr/Sources/Hedgewars/Hedgewars-GC/share/hedgewars/Data", "~/.hedgewars");
+    flibGetThemesList = (getThemesList_t*) hwlib.resolve("getThemesList");
+    flibFreeThemesList = (freeThemesList_t*) hwlib.resolve("freeThemesList");
+
+    flibInit("/usr/home/unC0Rr/Sources/Hedgewars/Hedgewars-GC/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars");
     flibRegisterGUIMessagesCallback(this, &guiMessagesCallback);
+
+    fillModels();
 }
 
 HWEngine::~HWEngine()
@@ -102,3 +109,18 @@
 {
     return QString::fromLatin1(flibGetSeed());
 }
+
+void HWEngine::fillModels()
+{
+    QStringList resultModel;
+
+    char ** themes = flibGetThemesList();
+    for (char **i = themes; *i != NULL; i++) {
+        QString theme = QString::fromUtf8(*i);
+
+        resultModel << theme;
+    }
+    flibFreeThemesList(themes);
+
+    m_engine->rootContext()->setContextProperty("themesModel", QVariant::fromValue(resultModel));
+}
--- a/qmlFrontend/hwengine.h	Sun Sep 28 21:33:14 2014 +0400
+++ b/qmlFrontend/hwengine.h	Tue Sep 30 00:54:04 2014 +0400
@@ -31,6 +31,7 @@
     QQmlEngine * m_engine;
 
     static void guiMessagesCallback(void * context, MessageType mt, const char * msg, uint32_t len);
+    void fillModels();
 
 private slots:
     void engineMessageHandler(MessageType mt, const QByteArray &msg);
--- a/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Sun Sep 28 21:33:14 2014 +0400
+++ b/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Tue Sep 30 00:54:04 2014 +0400
@@ -5,9 +5,9 @@
     HWButton {
         id: btnPreview
         x: 50
-        y: 66
-        width: 150
-        height: 150
+        y: 16
+        width: 256
+        height: 128
 
         onClicked: HWEngine.getPreview()
 
@@ -15,14 +15,27 @@
             target: HWEngine
             onPreviewImageChanged: previewImage.source = "image://preview/" + HWEngine.currentSeed()
         }
+
+        Image {
+            id: previewImage
+            x: 0
+            y: 0
+            width: 256
+            height: 128
+            cache: false
+        }
     }
 
-    Image {
-        id: previewImage
-        x: 210
-        y: 70
-        width: 256
-        height: 128
-        cache: false
+    ListView {
+        x: 330
+        y: 16
+        width: 100; height: 100
+
+        model: themesModel
+        delegate: Rectangle {
+            height: 25
+            width: 100
+            Text { text: modelData }
+        }
     }
 }