source: framspy/gui/libInterface.py @ 1198

Last change on this file since 1198 was 1198, checked in by Maciej Komosinski, 14 months ago

Added simple Python GUI for Framsticks library/server

File size: 4.7 KB
Line 
1from typing import Dict, List, Any, Tuple
2from gui.framsutils.FramsLib import FramsLib
3from gui.framsutils.creature import Creature
4from gui.framsutils.framsProperty import Property
5from gui.framsutils.FramsInterface import FramsInterface, TreeNode, InterfaceType
6
7class LibInterface(FramsInterface):
8        DEBUG_ADD_FIRST_CREATURE = False
9        DEBUG_FIRST_CREATURE = "fffX(fffIX[S:-1.556],RRlFFFMMMX[|-1:-10,3:10]llllFFMMMX[|1:-4.450]RlllFFFMMMX[|G:-1.404],fffIX[S:1])"
10
11        def __init__(self) -> None:
12                self.interfaceType = InterfaceType.LIB
13                self.frams: FramsLib = FramsLib()
14
15        def connect(self, address: str, port: int):
16                self.frams.initConnection(address)
17                if self.DEBUG_ADD_FIRST_CREATURE:
18                        self.frams.writeCreatureFromString(0, self.DEBUG_FIRST_CREATURE)
19                        self.frams.frams.GenePools[0].add(self.DEBUG_FIRST_CREATURE)
20
21        def disconnect(self):
22                self.frams.closeConnection()
23
24        def getError(self) -> None or str:
25                return self.frams.getError()
26
27        def start(self):
28                self.frams.start()
29
30        def stop(self):
31                self.frams.stop()
32
33        def step(self):
34                self.frams.step()
35
36        def listTree(self, path="/") -> List[Property]:
37                return self.frams.info(path)
38
39        def listTreeList(self, path) -> List[Property]:
40                return self.frams.infoList(path)
41
42        def makeInfoTree(self) -> TreeNode:
43                root = Property("", "/", "o")
44                childs = self.listTree()
45                tree = self._makeTree(None, root, childs)
46                tree.node.p["id"] = "/"
47                return tree
48       
49        def _makeTree(self, parent: TreeNode, node: Property, childs: List[Property]) -> TreeNode:
50                treeNode = TreeNode(node, None, parent)
51                p = parent
52                path = treeNode.node.p["id"]
53                while p:
54                        if p.node.p["id"] != '/':
55                                path = p.node.p["id"] + "/" + path
56                        p = p.parent
57
58                for child in childs:
59                        if child.p["id"] and child.p["name"] and child.p["type"]:
60                                if child.p["type"][0] == "o":
61                                        nodeChilds = self.listTree(path + "/" + child.p["id"])
62                                        c = self._makeTree(treeNode, child, nodeChilds)
63                                        treeNode.addChild(c)
64                                elif child.p["type"][0] == "l" and child.p["id"] == "groups":
65                                        nodeChilds = self.listTree(path + "/" + child.p["id"])
66                                        c = self._makeTreeFromList(treeNode, child, nodeChilds)
67                                        treeNode.addChild(c)
68                return treeNode
69
70        def _makeTreeFromList(self, parent: TreeNode, node: Property, childs: List[Property]) -> TreeNode:
71                treeNode = TreeNode(node, None, parent)
72                return treeNode
73
74        def readCreatures(self, colors = True) -> List[Creature]:
75                return self.frams.readCreatures("+", "+", colors)
76
77        def readGenePoolsGroups(self) -> Dict[str, int]:
78                return self.frams.readGenePoolsGroups()
79
80        def readGenePools(self, props: List[str]) -> Dict[str, List[Dict[str, Any]]]:
81                return self.frams.readGenePools(props)
82
83        def readPopulationsGroups(self) -> Dict[str, int]:
84                return self.frams.readPopulationsGroups()
85
86        def readPopulations(self, props: List[str]) -> Dict[str, List[Dict[str, Any]]]:
87                return self.frams.readPopulations(props)
88
89        def readCreatureDetails(self, group: int, index: int):
90                return self.frams.readCreatureInfo(group, index)
91
92        def readGenotypeDetails(self, group: int, index: int):
93                return self.frams.readGenotypeInfo(group, index)
94
95        def readParameterDetails(self, prop: str):
96                return self.frams.readPropertyInfo(prop)
97
98        def writeCreatureDetail(self, creatureNo: str, prop: str, value: str):
99                return self.frams.writeCreatureInfo(creatureNo, prop, value)
100
101        def writeGenotypeDetail(self, genotypeNo: str, prop: str, value: str):
102                return self.frams.writeGenotypeInfo(genotypeNo, prop, value)
103
104        def writeParameterDetail(self, path: str, prop: str, value: str):
105                return self.frams.writePropertyInfo(path, prop, value)
106
107        def getMotd(self) -> str:
108                return ""
109
110        def getSimulationStatus(self) -> bool:
111                return self.frams.getSimulationStatus()
112
113        def loadFile(self, path: str) -> None:
114                self.frams.loadFile(path)
115
116        def importFile(self, path: str, options: int) -> None:
117                self.frams.importFile(path, options)
118
119        def saveFile(self, path: str, options: int) -> None:
120                self.frams.saveFile(path, options)
121
122        def getWorldType(self) -> int:
123                return self.frams.getWorldType()
124
125        def getWorldSize(self) -> float:
126                return self.frams.getWorldSize()
127
128        def getWorldWaterLevel(self) -> float:
129                return self.frams.getWorldWaterLevel()
130
131        def getWorldBoundaries(self) -> int:
132                return self.frams.getWorldBoundaries()
133
134        def getWorldMap(self) -> str:
135                return self.frams.getWorldMap()
136
137        def getSimtype(self) -> int:
138                return self.frams.getSimtype()
139
140        def getFPSDefinitions(self) -> List[Tuple[int, int]]:
141                return [(2, 1),
142                                (5, 1),
143                                (10, 1),
144                                (25, 1),
145                                (50, 1),
146                                (-1, 1),
147                                (-1, 2),
148                                (-1, 3),
149                                (-1, 4),
150                                (-1, 10),
151                                (-1, 100),
152                                (-1, 1000)]
Note: See TracBrowser for help on using the repository browser.