def GetTextureFromURL(self, path, currentURL=None, ignoreCache=0, dontcache=0, fromWhere=None, sizeonly=0, retry=1): if path.endswith('.blue'): return self.GetPic_blue(path) fullPath = corebrowserutil.ParseURL(path, currentURL)[0] if path.startswith('res:'): try: surface = trinity.Tr2HostBitmap() surface.CreateFromFile(path) w, h = surface.width, surface.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) if sizeonly: return (path, w, h, bw, bh) return self.ReturnTexture(path, w, h, bw, bh) except: self.LogError('Failed to load image', path) if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self.ErrorPic(sizeonly) if ignoreCache: sm.GetService('browserCache').InvalidateImage(fullPath) while self.urlloading.has_key(fullPath): blue.pyos.BeNice() if not dontcache: cacheData = sm.GetService('browserCache').GetFromCache(fullPath) if cacheData and os.path.exists(cacheData[0].replace( 'cache:/', blue.paths.ResolvePath(u'cache:/'))): if sizeonly: return cacheData return self.ReturnTexture(*cacheData) try: self.urlloading[fullPath] = 1 ret = corebrowserutil.GetStringFromURL(fullPath) cacheID = int( str(blue.os.GetWallclockTime()) + str(uthread.uniqueId() or uthread.uniqueId())) imagestream = ret.read() ext = None if 'content-type' in ret.headers.keys( ) and ret.headers['content-type'].startswith('image/'): ext = ret.headers['content-type'][6:] if ext == None or ext == 'png': header = imagestream[:16] for sig, sext in [('PNG', 'PNG'), ('GIF', 'GIF'), ('JFI', 'JPEG'), ('BM8', 'BMP')]: for i in xrange(0, 12): if header[i:i + 3] == sig: ext = sext break if not ext: header = imagestream[-16:] for sig, sext in [('XFILE', 'TGA')]: for i in xrange(0, 10): if header[i:i + 5] == sig: ext = sext break if ext: filename = '%sBrowser/Img/%s.%s' % ( blue.paths.ResolvePath(u'cache:/'), cacheID, ext) resfile = blue.classes.CreateInstance('blue.ResFile') if not resfile.Open(filename, 0): resfile.Create(filename) resfile.Write(imagestream) resfile.Close() if ext.upper() == 'GIF': g = DrawArea() g.setBgColor(Transparent) g.loadGIF(filename.replace(u'/', u'\\').encode('utf8')) ext = 'PNG' filename = u'%sBrowser/Img/%s.%s' % ( blue.paths.ResolvePath(u'cache:/'), cacheID, ext) g.outPNG(filename.replace(u'/', u'\\').encode('utf8')) surface = trinity.Tr2HostBitmap() surface.CreateFromFile(filename) w, h = surface.width, surface.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) cachePath = 'cache:/Browser/Img/%s.%s' % (cacheID, ext) if 'pragma' not in ret.headers.keys( ) or ret.headers['Pragma'].find('no-cache') == -1: sm.GetService('browserCache').Cache( fullPath, (cachePath, w, h, bw, bh)) del self.urlloading[fullPath] if sizeonly: return (cachePath, w, h, bw, bh) return self.ReturnTexture(cachePath, w, h, bw, bh) del self.urlloading[fullPath] return self.ErrorPic(sizeonly) except Exception as e: if retry: sys.exc_clear() if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] return self.GetTextureFromURL(path, currentURL, ignoreCache, dontcache, fromWhere, sizeonly, 0) self.LogError(e, 'Failed to load image', repr(path)) if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self.ErrorPic(sizeonly)
def DrawSolarsystem(self, sprite, ids, imageid, mapitems, SIZE): if not len(mapitems): return planets = [] stargates = [] asteroidbelts = [] for item in mapitems: if item.groupID == const.groupPlanet: planets.append(item) elif item.groupID == const.groupStargate: stargates.append(item) elif item.groupID == const.groupAsteroidBelt: asteroidbelts.append(item) drawarea = DrawArea() drawarea.setTransparentColor(-1) drawarea.setSize(SIZE, SIZE, 4278190080L) cords = {} sunID = None maxdist = 0.0 for item in mapitems: pos = (item.x, 0.0, item.z) maxdist = max(maxdist, geo2.Vec3Length(pos)) if item.groupID == const.groupSun: sunID = item.itemID radius = 3 drawarea.circle(SIZE / 2, SIZE / 2, radius, radius, 10066329, 10066329) sizefactor = SIZE / 2 / maxdist * self.fillSize self.sr.sizefactor = sizefactor self.sr.sizefactorsize = SIZE if self.allowAbstract and settings.user.ui.Get('solarsystemmapabstract', 0): _planets = [] for planet in planets: pos = (planet.x, planet.y, planet.z) dist = geo2.Vec3Length(pos) _planets.append([dist, planet]) _planets = uiutil.SortListOfTuples(_planets) planet = _planets i = 1 for item in planets: pos = (item.x, 0.0, item.z) dist = geo2.Vec3Length(pos) if self.allowAbstract and settings.user.ui.Get('solarsystemmapabstract', 0): planetscale = i * (maxdist / len(planets)) / dist pos = geo2.Vec3Scale(pos, planetscale) x = FLIPMAP * pos[0] * sizefactor + SIZE / 2 y = pos[2] * sizefactor + SIZE / 2 radius = 1 cords[item.itemID] = (x, SIZE - y, radius) drawarea.circle(x, SIZE - y, radius, radius, 16777215, mathUtil.LtoI(4278190080L)) self.AddChilds(x, y, radius, item.itemID, SIZE, drawarea, cords, item) i += 1 self.orbs = [] for orbit in planets: if orbit.itemID in cords: x, y, radius = cords[orbit.itemID] center = SIZE / 2 frompos = geo2.Vector(float(center), 0.0, float(center)) topos = geo2.Vector(float(x), 0.0, float(y)) diff = topos - frompos rad = int(geo2.Vec3Length(diff)) drawarea.circle(center, center, rad, rad, self.GetColorByGroupID(const.groupPlanet), mathUtil.LtoI(4278190080L)) orbpos = (orbit.x, 0.0, orbit.z) orbdist = geo2.Vec3Length(orbpos) self.orbs.append([orbdist, (orbdist, rad, orbit, SIZE)]) self.orbs = uiutil.SortListOfTuples(self.orbs) for item in stargates: if self.allowAbstract and settings.user.ui.Get('solarsystemmapabstract', 0): if not len(self.orbs): return x, y = self.GetAbstractPosition((item.x, 0.0, item.z)) else: x = FLIPMAP * self.sr.sizefactor * item.x + self.sr.sizefactorsize / 2 y = self.sr.sizefactor * item.z + self.sr.sizefactorsize / 2 x += 6 radius = 1 drawarea.circle(x, SIZE - y, radius, radius, 0, self.GetColorByGroupID(const.groupStargate)) cords[item.itemID] = (x, SIZE - y, radius) self.areas = [ (cords[id][0], cords[id][1], cords[id][2], id) for id in cords.iterkeys() ] self.cordsAsPortion = {} for id in cords.iterkeys(): self.cordsAsPortion[id] = (cords[id][0] / float(SIZE), cords[id][1] / float(SIZE)) if self.destroyed: return self.PlaceBackground('res:/UI/Texture/map_ssunderlay.png') self.PlaceMap(sprite, drawarea, SIZE)
def Draw(self, ids, idlevel, drawlevel, needsize, sprite = None, solarsystem = None): _settings = (ids, idlevel, drawlevel, needsize) if _settings == self.settings: return self.settings = _settings lg.Info('2Dmaps', 'Drawing map, ids/idlevel/drawlevel:', ids, idlevel, drawlevel) if drawlevel <= idlevel: return if drawlevel == DRAW_SOLARSYSTEM_INTERIOR and len(ids) > 1: ids = ids[:1] SIZE = needsize if sprite is None: sprite = self.sprite _ids = {} for id in ids: _ids[id] = '' ids = _ids.keys() endid = '' if len(ids) > 1: endid = '%s_' % ids[-1] self.ids = ids self.idlevel = idlevel self.drawlevel = drawlevel self.needsize = needsize imageid = '%s_%s_%s_%s_%s_%s' % (ids[0], '_' * max(0, len(ids) - 2), endid, idlevel, drawlevel, self.fillSize) imageid = imageid.replace('.', '') if self.drawlevel == DRAW_SOLARSYSTEM_INTERIOR: imageid += '_' + str(settings.user.ui.Get('solarsystemmapabstract', 0)) lg.Info('2Dmaps', 'MapID is: %s' % imageid) for each in self.overlays.children: if each.name == 'destinationline': each.renderObject = None each.Close() self.cordsAsPortion = {} mapitems = self.mapitems = self.GetMapData(ids, idlevel, drawlevel) if drawlevel == 4: self.DrawSolarsystem(sprite, ids, imageid, mapitems, SIZE) self.CheckMyLocation(solarsystem=solarsystem) return connections, outsideitems = self.GetConnectionData(ids, idlevel, drawlevel) self.outsideitems = outsideitems minx = 1e+100 maxx = -1e+100 minz = 1e+100 maxz = -1e+100 for item in mapitems: minx = min(minx, item.x) maxx = max(maxx, item.x) minz = min(minz, item.z) maxz = max(maxz, item.z) mw = -minx + maxx mh = -minz + maxz if not (mw and mh): return SIZE = SIZE * 2 drawarea = DrawArea() drawarea.setTransparentColor(-1) drawarea.setSize(SIZE, SIZE, 4278190080L) dotrad = [2, 3, 4, 5, 6][idlevel] sizefactor = min(SIZE / mw, SIZE / mh) * self.portion cords = {} for item in mapitems[:]: if item.groupID == const.groupRegion: if drawlevel != 1: continue if item.groupID == const.groupConstellation: if drawlevel != 2: continue x = int(item.x * sizefactor - int(minx * sizefactor) + (SIZE - mw * sizefactor) / 2) y = int(item.z * sizefactor - int(minz * sizefactor) + (SIZE - mh * sizefactor) / 2) cords[item.itemID] = (x, SIZE - y, dotrad, 1, 16777215) for item in self.outsideitems: x = int(item.x * sizefactor - int(minx * sizefactor) + (SIZE - mw * sizefactor) / 2) y = int(item.z * sizefactor - int(minz * sizefactor) + (SIZE - mh * sizefactor) / 2) cords[item.itemID] = (x, SIZE - y, dotrad, 0, None) done = [] i = 0 lineWidth = 2.0 for jumptype in connections: for pair in jumptype: fr, to = pair if (fr, to) in done: continue if fr in cords and to in cords: drawarea.line(cords[fr][0], cords[fr][1], cords[to][0], cords[to][1], [43520, 255, 16711680][i], lineWidth) drawarea.line(cords[fr][0] + 1, cords[fr][1], cords[to][0] + 1, cords[to][1], [43520, 255, 16711680][i], lineWidth) drawarea.line(cords[fr][0], cords[fr][1] + 1, cords[to][0], cords[to][1] + 1, [43520, 255, 16711680][i], lineWidth) i += 1 for x, y, dotrad, cordtype, col in cords.itervalues(): if cordtype == 0: dotrad = dotrad / 2 drawarea.circle(x, y, dotrad, dotrad, 16777215, 16777215) self.areas = [ (cords[id][0], cords[id][1], cords[id][2], id) for id in cords.iterkeys() ] self.cordsAsPortion = {} for id in cords.iterkeys(): self.cordsAsPortion[id] = (cords[id][0] / float(SIZE), cords[id][1] / float(SIZE)) self.CheckMyLocation(solarsystem=solarsystem) self.CheckDestination() self.PlaceMap(sprite, drawarea, SIZE)
def GetTextureFromURL(self, path, currentURL = None, ignoreCache = 0, dontcache = 0, fromWhere = None, sizeonly = 0, retry = 1): if path.endswith('.blue'): return self.GetPic_blue(path) fullPath = corebrowserutil.ParseURL(path, currentURL)[0] if path.startswith('res:'): try: surface = trinity.Tr2HostBitmap() surface.CreateFromFile(path) w, h = surface.width, surface.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) if sizeonly: return (path, w, h, bw, bh) return self.ReturnTexture(path, w, h, bw, bh) except: self.LogError('Failed to load image', path) if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self.ErrorPic(sizeonly) if ignoreCache: sm.GetService('browserCache').InvalidateImage(fullPath) while self.urlloading.has_key(fullPath): blue.pyos.BeNice() if not dontcache: cacheData = sm.GetService('browserCache').GetFromCache(fullPath) if cacheData and os.path.exists(cacheData[0].replace('cache:/', blue.paths.ResolvePath(u'cache:/'))): if sizeonly: return cacheData return self.ReturnTexture(*cacheData) try: self.urlloading[fullPath] = 1 ret = corebrowserutil.GetStringFromURL(fullPath) cacheID = int(str(blue.os.GetWallclockTime()) + str(uthread.uniqueId() or uthread.uniqueId())) imagestream = ret.read() ext = None if 'content-type' in ret.headers.keys() and ret.headers['content-type'].startswith('image/'): ext = ret.headers['content-type'][6:] if ext == None or ext == 'png': header = imagestream[:16] for sig, sext in [('PNG', 'PNG'), ('GIF', 'GIF'), ('JFI', 'JPEG'), ('BM8', 'BMP')]: for i in xrange(0, 12): if header[i:i + 3] == sig: ext = sext break if not ext: header = imagestream[-16:] for sig, sext in [('XFILE', 'TGA')]: for i in xrange(0, 10): if header[i:i + 5] == sig: ext = sext break if ext: filename = '%sBrowser/Img/%s.%s' % (blue.paths.ResolvePath(u'cache:/'), cacheID, ext) resfile = blue.classes.CreateInstance('blue.ResFile') if not resfile.Open(filename, 0): resfile.Create(filename) resfile.Write(imagestream) resfile.Close() if ext.upper() == 'GIF': g = DrawArea() g.setBgColor(Transparent) g.loadGIF(filename.replace(u'/', u'\\').encode('utf8')) ext = 'PNG' filename = u'%sBrowser/Img/%s.%s' % (blue.paths.ResolvePath(u'cache:/'), cacheID, ext) g.outPNG(filename.replace(u'/', u'\\').encode('utf8')) surface = trinity.Tr2HostBitmap() surface.CreateFromFile(filename) w, h = surface.width, surface.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) cachePath = 'cache:/Browser/Img/%s.%s' % (cacheID, ext) if 'pragma' not in ret.headers.keys() or ret.headers['Pragma'].find('no-cache') == -1: sm.GetService('browserCache').Cache(fullPath, (cachePath, w, h, bw, bh)) del self.urlloading[fullPath] if sizeonly: return (cachePath, w, h, bw, bh) return self.ReturnTexture(cachePath, w, h, bw, bh) del self.urlloading[fullPath] return self.ErrorPic(sizeonly) except Exception as e: if retry: sys.exc_clear() if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] return self.GetTextureFromURL(path, currentURL, ignoreCache, dontcache, fromWhere, sizeonly, 0) self.LogError(e, 'Failed to load image', repr(path)) if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self.ErrorPic(sizeonly)