Schemes list combobox with their names qmlfrontend
authorunc0rr
Thu, 18 Dec 2014 00:03:53 +0300 (2014-12-17)
branchqmlfrontend
changeset 10616 20a2d5e6930a
parent 10614 4afb4c4bf495
child 10748 dc587913987c
Schemes list combobox with their names
hedgewars/CMakeLists.txt
hedgewars/hwLibrary.pas
hedgewars/uFLSchemes.pas
hedgewars/uFLUtils.pas
qmlFrontend/flib.h
qmlFrontend/hwengine.cpp
qmlFrontend/qml/qmlFrontend/GameConfig.qml
--- a/hedgewars/CMakeLists.txt	Sun Dec 07 22:22:39 2014 +0300
+++ b/hedgewars/CMakeLists.txt	Thu Dec 18 00:03:53 2014 +0300
@@ -104,6 +104,7 @@
     uFLGameConfig.pas
     uFLIPC.pas
     uFLScripts.pas
+    uFLSchemes.pas
     uFLTeams.pas
     uFLTypes.pas
     uFLUtils.pas
--- a/hedgewars/hwLibrary.pas	Sun Dec 07 22:22:39 2014 +0300
+++ b/hedgewars/hwLibrary.pas	Thu Dec 18 00:03:53 2014 +0300
@@ -45,6 +45,7 @@
     , uFLData
     , uFLTeams
     , uFLScripts
+    , uFLSchemes
     ;
 
 {$INCLUDE "config.inc"}
@@ -161,6 +162,7 @@
     freeThemesList,
     getThemeIcon,
     getScriptsList,
+    getSchemesList,
     getTeamsList,
     tryAddTeam,
     tryRemoveTeam,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uFLSchemes.pas	Thu Dec 18 00:03:53 2014 +0300
@@ -0,0 +1,155 @@
+unit uFLSchemes;
+interface
+uses uFLTypes;
+
+function getSchemesList: PPChar; cdecl;
+procedure freeSchemesList;
+
+implementation
+uses uFLUtils, uFLIPC, uPhysFSLayer, uFLData;
+
+const MAX_SCHEME_NAMES = 64;
+type
+    TScheme = record
+            schemeName: shortstring;
+        end;
+    PScheme = ^TScheme;
+    TSchemeArray = array [0..0] of TScheme;
+    PSchemeArray = ^TSchemeArray;
+var
+    schemesList: PScheme;
+    schemesNumber: LongInt;
+    listOfSchemeNames: array[0..MAX_SCHEME_NAMES] of PChar;
+
+procedure loadSchemes;
+var f: PFSFile;
+    scheme: PScheme;
+    schemes: PSchemeArray;
+    s: shortstring;
+    l, i: Longword;
+begin
+    f:= pfsOpenRead('/Config/schemes.ini');
+    schemesNumber:= 0;
+
+    if f <> nil then
+    begin
+        while (not pfsEOF(f)) and (schemesNumber = 0) do
+        begin
+            pfsReadLn(f, s);
+
+            if copy(s, 1, 5) = 'size=' then
+                schemesNumber:= strToInt(midStr(s, 6));
+        end;
+
+        //inc(schemesNumber); // add some default schemes
+
+        schemesList:= GetMem(sizeof(schemesList^) * (schemesNumber + 1));
+        schemes:= PSchemeArray(schemesList);
+
+        while (not pfsEOF(f)) do
+        begin
+            pfsReadLn(f, s);
+
+            i:= 1;
+            while(i <= length(s)) and (s[i] <> '\') do inc(i);
+
+            if i < length(s) then
+            begin
+                l:= strToInt(copy(s, 1, i - 1));
+
+                if (l < schemesNumber) and (l > 0) then
+                begin
+                    scheme:= @schemes^[l - 1];
+
+                    if copy(s, i + 1, 5) = 'name=' then
+                        scheme^. schemeName:= midStr(s, i + 6);
+                end;
+            end;
+        end;
+
+        pfsClose(f)
+    end;
+{
+name=AI TEST
+fortsmode=false
+divteams=false
+solidland=false
+border=false
+lowgrav=false
+laser=false
+invulnerability=false
+mines=true
+damagefactor=100
+turntime=40
+health=100
+suddendeath=0
+caseprobability=5
+vampiric=false
+karma=false
+artillery=false
+minestime=0
+landadds=4
+randomorder=true
+king=false
+placehog=false
+sharedammo=false
+disablegirders=false
+minedudpct=100
+explosives=40
+disablelandobjects=true
+aisurvival=true
+resethealth=false
+infattack=true
+resetweps=true
+perhogammo=false
+minesnum=0
+healthprobability=100
+healthcaseamount=50
+waterrise=0
+healthdecrease=0
+disablewind=false
+morewind=false
+ropepct=100
+tagteam=false
+getawaytime=100
+bottomborder=false
+worldedge=1
+scriptparam=@Invalid()
+}
+end;
+
+
+function getSchemesList: PPChar; cdecl;
+var i, t, l: Longword;
+    scheme: PScheme;
+begin
+    if schemesList = nil then
+        loadSchemes;
+
+    t:= schemesNumber;
+    if t >= MAX_SCHEME_NAMES then 
+        t:= MAX_SCHEME_NAMES;
+
+    scheme:= schemesList;
+    for i:= 0 to Pred(t) do
+    begin
+        l:= length(scheme^.schemeName);
+        if l >= 255 then l:= 254;
+        scheme^.schemeName[l + 1]:= #0;
+        listOfSchemeNames[i]:= @scheme^.schemeName[1];
+        inc(scheme)
+    end;
+
+    listOfSchemeNames[t]:= nil;
+
+    getSchemesList:= listOfSchemeNames
+end;
+
+
+procedure freeSchemesList;
+begin
+    if schemesList <> nil then
+        FreeMem(schemesList, sizeof(schemesList^) * schemesNumber)
+end;
+
+end.
--- a/hedgewars/uFLUtils.pas	Sun Dec 07 22:22:39 2014 +0300
+++ b/hedgewars/uFLUtils.pas	Thu Dec 18 00:03:53 2014 +0300
@@ -3,6 +3,7 @@
 
 function str2PChar(const s: shortstring): PChar;
 function intToStr(n: LongInt): shortstring;
+function strToInt(s: shortstring): LongInt;
 function midStr(s: shortstring; pos: byte): shortstring;
 procedure underScore2Space(var s: shortstring);
 
@@ -27,6 +28,11 @@
     str(n, intToStr)
 end;
 
+function strToInt(s: shortstring): LongInt;
+begin
+val(s, strToInt);
+end;
+
 function midStr(s: shortstring; pos: byte): shortstring;
 begin
     midStr:= copy(s, pos, length(s) - pos + 1)
--- a/qmlFrontend/flib.h	Sun Dec 07 22:22:39 2014 +0300
+++ b/qmlFrontend/flib.h	Thu Dec 18 00:03:53 2014 +0300
@@ -45,6 +45,7 @@
 typedef uint32_t getThemeIcon_t(char * theme, char * buffer, uint32_t size);
 
 typedef char **getScriptsList_t();
+typedef char **getSchemesList_t();
 
 typedef char **getTeamsList_t();
 typedef void tryAddTeam_t(const char * teamName);
--- a/qmlFrontend/hwengine.cpp	Sun Dec 07 22:22:39 2014 +0300
+++ b/qmlFrontend/hwengine.cpp	Thu Dec 18 00:03:53 2014 +0300
@@ -25,6 +25,7 @@
     freeThemesList_t *flibFreeThemesList;
     getThemeIcon_t *flibGetThemeIcon;
     getScriptsList_t *flibGetScriptsList;
+    getSchemesList_t *flibGetSchemesList;
     getTeamsList_t *flibGetTeamsList;
     tryAddTeam_t * flibTryAddTeam;
     tryRemoveTeam_t * flibTryRemoveTeam;
@@ -62,6 +63,7 @@
     flibGetThemeIcon = (getThemeIcon_t*) hwlib.resolve("getThemeIcon");
 
     flibGetScriptsList = (getScriptsList_t*) hwlib.resolve("getScriptsList");
+    flibGetSchemesList = (getSchemesList_t*) hwlib.resolve("getSchemesList");
 
     flibResetGameConfig = (resetGameConfig_t*) hwlib.resolve("resetGameConfig");
     flibGetTeamsList = (getTeamsList_t*) hwlib.resolve("getTeamsList");
@@ -171,25 +173,25 @@
     QStringList resultModel;
 
     char ** themes = flibGetThemesList();
-    for (char **i = themes; *i != NULL; i++) {
-        QString theme = QString::fromUtf8(*i);
-
-        resultModel << theme;
-    }
+    for (char **i = themes; *i != NULL; i++)
+        resultModel << QString::fromUtf8(*i);
     flibFreeThemesList(themes);
 
     m_engine->rootContext()->setContextProperty("themesModel", QVariant::fromValue(resultModel));
 
+    // scripts model
     resultModel.clear();
-    char ** scripts = flibGetScriptsList();
-
-    for (char **i = scripts; *i != NULL; i++) {
-        QString script = QString::fromUtf8(*i);
-
-        resultModel << script;
-    }
+    for (char **i = flibGetScriptsList(); *i != NULL; i++)
+        resultModel << QString::fromUtf8(*i);
 
     m_engine->rootContext()->setContextProperty("scriptsModel", QVariant::fromValue(resultModel));
+
+    // schemes model
+    resultModel.clear();
+    for (char **i = flibGetSchemesList(); *i != NULL; i++)
+        resultModel << QString::fromUtf8(*i);
+
+    m_engine->rootContext()->setContextProperty("schemesModel", QVariant::fromValue(resultModel));
 }
 
 void HWEngine::getTeamsList()
--- a/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Sun Dec 07 22:22:39 2014 +0300
+++ b/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Thu Dec 18 00:03:53 2014 +0300
@@ -101,6 +101,38 @@
         }
     }
 
+    HWComboBox {
+        id: cbScheme
+        x: 50
+        y: 336
+        width: 256
+        height: 64
+
+        model: schemesModel
+        delegate: Rectangle {
+            height: 25
+            width: 100
+            color: "transparent"
+
+            property string itemIconSource: ""
+            property alias itemText: schemeName.text
+
+            Row {
+                //Image {id: themeIcon; width: height; height: parent.height; source: "image://theme/" + modelData}
+                Text {id: schemeName; text: modelData }
+            }
+
+            MouseArea {
+                 z: 1
+                 anchors.fill: parent
+                 onClicked: {
+                     cbScheme.currentIndex = index
+                     //HWEngine.setScheme(schemeName.text)
+                 }
+            }
+        }
+    }
+
 
     ListView {
         id: playingTeamsList