def transferImageTile(objref, indexstr, targetimage, targetcoords, outfilename): value = objref.ask(indexstr) if value != None: imgref = simutools.SimutransImgParam(value) imgname = os.path.join(os.path.dirname(objref.srcfile), imgref.file + ".png") if not imgname in loadedimages: loadedimages[imgname] = pygame.image.load(imgname) srccoords = pygame.Rect(imgref.coords[1] * paksize, imgref.coords[0] * paksize, paksize, paksize) imgref.coords[0] = targetcoords[1] imgref.coords[1] = targetcoords[0] off_move_x = (outsize - paksize) / 2 off_move_y = 3 * (outsize - paksize) / 4 imgref.offset[0] -= off_move_x imgref.offset[1] -= off_move_y targetcoords[0] = targetcoords[0] * outsize + off_move_x targetcoords[1] = targetcoords[1] * outsize + off_move_y targetimage.blit(loadedimages[imgname], targetcoords, srccoords) # copy image tile imgref.file = outfilename objref.put(indexstr, str(imgref))
def get_png_tile(refstr, base, tilesize=paksize) : # always returns a surface imgref = simutools.SimutransImgParam(refstr) image = pygame.Surface((tilesize, tilesize)) image.fill((231,255,255)) if imgref.file != "-" : # guaranteed to get here "-", correct name or other bug filepath = join(os.path.dirname(base), imgref.file + ".png") count = 0 source = None if png_cache_size > 0 : try : source,count = imgcache[filepath] except : # not cached source = pygame.image.load(filepath) # will add to cache, prune first if len(imgcache) > png_cache_size-1 : min_cnt = 65536 # infinity min_pth = "" for filepath,data in imgcache.items() : if data[1] < min_cnt : min_cnt = data[1] min_pth = filepath del imgcache[min_pth] imgcache[filepath] = (source,count+1); else : # caching off source = pygame.image.load(filepath) srccoords = pygame.Rect(imgref.coords[1] * tilesize, imgref.coords[0] * tilesize, tilesize, tilesize) image.blit(source, [0,0], srccoords) return image
def procObj(obj) : parentdir = os.path.dirname(obj.srcfile) # lazy loading tile sizes for cache paksize = PakSizes[parentdir] if not paksize : # uninitialized = 0 paksize = get_paksize(parentdir) PakSizes[parentdir] = paksize references = [[None, obj.ask("Icon")], [None, obj.ask("Cursor")]] # content is otherwise list of [indices,reference] -> normalize the two odd entries for param in simutools.ImageParameterNames : references += obj.ask_indexed(param) for ref in references : imgref = simutools.SimutransImgParam(ref[1]) if not imgref.isEmpty() : imgref.paksize = paksize # attaching new field not present in original class, remember! imagefile = os.path.normpath(os.path.join(parentdir, imgref.file)) Images[imagefile].append(imgref)
def procObj(obj): objname = obj.ask("name") print "processing", objname if obj.ask("type", "none").lower() in [ "res", "com", "ind", "cur" ]: # needs default string because of lower() dims = obj.ask("dims", "1,1,1") dims = dims.split(",") if len(dims) == 2: dims.append("1") # dims converted to canonical form [?, ?, ?] of strings rots = int(dims[2]) has_winter = False max_height = 0 max_anim = 0 images = obj.ask_indexed("backimage") # preparsing pass - find what are the features of object: seasons, animation... for item in images: # tuples [params] = value -> (params, value) indices = item[0].split("][") # first and last item still have the single bracket! if len(indices) > 0: indices[0] = indices[0][1:] indices[-1] = indices[-1][:-1] # so fix them ^ if len(indices) == 6: has_winter = indices[5] == "1" or has_winter max_anim = max(int(indices[4]), max_anim) max_height = max(int(indices[3]), max_height) # now we know the numbers so we can select new image layout etc. if max_anim > 0: # animated? # primary animation, secondary rotations, then snow and height layout = 0 new_height = 1 + (max_height + 1) * rots * ( 2 if has_winter else 1) # first +1 for text row new_width = max((max_anim + 1), minwidth) elif rots > 1: # no anim. -> rotations? # primary rotations, secondary snow and height layout = 1 new_height = 1 + (max_height + 1) * (2 if has_winter else 1) new_width = max(rots, minwidth) else: # no anim or rotation? # primary climates (if any!), secondary height layout = 2 new_height = 1 + (max_height + 1) new_width = max((2 if has_winter else 1), minwidth) newimage = pygame.Surface((paksize * new_width, paksize * new_height)) newimage.fill((231, 255, 255)) # background newimage.fill( (255, 255, 255), (0, 0, new_width * paksize, paksize)) # description stripe text = "name: " + obj.ask("name", "?") surf = font.render(text, False, (0, 0, 0), (255, 255, 255)) # black text on white newimage.blit(surf, (10, 10)) text = "copyright: " + obj.ask("copyright", "?") surf = font.render(text, False, (0, 0, 0), (255, 255, 255)) # black text on white newimage.blit(surf, (10, 10 + fntsize + 4)) cname = simutools.canonicalObjName(objname) # blitting pass - copy the parts onto new picture for rot in xrange(rots): for z in xrange( max_height + 1 ): # +1 because we read maximal index, not number of them! for t in xrange(max_anim + 1): # +1 same here indexstr = "backimage[%i][0][0][%i][%i]" % (rot, z, t) value = obj.ask(indexstr) if value == None: # failed but asking is precise, so ask again for explicit number with no winter indexstr = "backimage[%i][0][0][%i][%i][0]" % (rot, z, t) value = obj.ask(indexstr) # now it must be a string! or a mising image if value != None: # the good case imgref = simutools.SimutransImgParam(value) imgname = os.path.join(os.path.dirname(obj.srcfile), imgref.file + ".png") if not loadedimages.has_key(imgname): loadedimages[imgname] = pygame.image.load(imgname) srccoords = pygame.Rect(imgref.coords[1] * paksize, \ imgref.coords[0] * paksize, paksize, paksize) # calculate position on old image if layout == 2: targetcoords = [0, 1 + max_height - z] elif layout == 1: targetcoords = [rot, 1 + max_height - z] else: # 0 targetcoords = [ t, 1 + (max_height - z) * (rot + 1) * (2 if has_winter else 1) ] imgref.coords[0] = targetcoords[1] imgref.coords[1] = targetcoords[0] targetcoords[0] *= paksize targetcoords[1] *= paksize newimage.blit(loadedimages[imgname], targetcoords, srccoords) # copy image tile imgref.file = cname obj.put(indexstr, str(imgref)) else: # say if it is mising! print "Missing image", indexstr if has_winter: indexstr = "backimage[%i][0][0][%i][%i][1]" % (rot, z, t) value = obj.ask(indexstr) if value != None: imgref = simutools.SimutransImgParam(value) imgname = os.path.join( os.path.dirname(obj.srcfile), imgref.file + ".png") if not loadedimages.has_key(imgname): loadedimages[imgname] = pygame.image.load( imgname) srccoords = pygame.Rect(imgref.coords[1] * paksize, \ imgref.coords[0] * paksize, paksize, paksize) if layout == 2: targetcoords = [1, 1 + max_height - z] elif layout == 1: targetcoords = [ rot, 1 + (max_height + 1) * 2 - 1 - z ] else: # 0 targetcoords = [ t, 1 + (max_height * 2 - z) * (rot + 1) * (2 if has_winter else 1) ] imgref.coords[0] = targetcoords[1] imgref.coords[1] = targetcoords[0] targetcoords[0] *= paksize targetcoords[1] *= paksize newimage.blit(loadedimages[imgname], targetcoords, srccoords) imgref.file = cname obj.put(indexstr, str(imgref)) else: # misssing winter image even if winter declared print "Missing image", indexstr pygame.image.save(newimage, os.path.join(outdir, cname + ".png")) f = open(os.path.join(outdir, cname + ".dat"), 'w') for l in obj.lines: f.write(l) f.close()
def procObj(i): obj = Data[i] objname = obj.ask("name") intro_year = int(obj.ask("intro_year", 0)) intro_month = int(obj.ask("intro_month", 1)) - 1 retire_year = int(obj.ask("retire_year", stop_year)) retire_month = int(obj.ask("retire_month", 1)) - 1 bin_number = i / bin_height # which bin is this in bin_position = i % bin_height # which item is this in that bin # bar color codes information about the vehicle: data_color = data_color_normal # start with default state (from-to) # check timeline state if (intro_year == 0) and (retire_year == 0): data_color = data_color_always intro_year = start_year intro_month = 0 retire_year = end_year retire_month = 12 elif intro_year == 0: data_color = data_color_nointro intro_year = start_year intro_month = 0 elif retire_year == stop_year: data_color = data_color_noretire retire_year = end_year retire_month = 12 else: pass # nothing to do oterwise # knowing if the vehicle is not free is still more important if check_authors: if not CheckAuthors(obj.ask("copyright", "", False)): data_color = data_color_unfree # compat.tab checking overrides all since it is the most interesting info overall if check_compat: if compat.has_key(objname + "\n"): data_color = data_color_replaced is_disabled = (intro_year * 12 + intro_month) >= (retire_year * 12 + retire_month) # important to think about these cases - they can stretch timeline really loooong into future and increase image size -> memory... start_x = left_margin + ( (intro_year - start_year) * 12 + intro_month) * month_width end_x = left_margin + ( (retire_year - start_year) * 12 + retire_month) * month_width start_y = vertical_margin + bin_number * bin_height_px + bin_text_space + bin_position * item_height + 1 end_y = start_y + item_height - 2 if not is_disabled: canvas.fill(data_color, (start_x, start_y, end_x - start_x, end_y - start_y)) speed_length = int( obj.ask("speed", 0)) * parameter_bar_max / max_speed if max_speed > 0 else 0 power_length = ( int(obj.ask("power", 0)) * int(obj.ask("gear", 100)) ) * parameter_bar_max / 100 / max_power if max_power > 0 else 0 # integer arithmetic - multiplication always first! start = (start_x, start_y) end = (start_x + speed_length, start_y) pygame.draw.line(canvas, speed_color, start, end, 2) start = (start_x, end_y - 2) end = (start_x + power_length, end_y - 2) pygame.draw.line(canvas, power_color, start, end, 2) else: pass # charting done, now picture and labels picture_txt_ref = None if not picture_txt_ref: picture_txt_ref = obj.ask("freightimage[0][sw]") if not picture_txt_ref: picture_txt_ref = obj.ask("freightimage[0][ne]") if not picture_txt_ref: picture_txt_ref = obj.ask("freightimage[sw]") if not picture_txt_ref: picture_txt_ref = obj.ask("freightimage[ne]") if not picture_txt_ref: picture_txt_ref = obj.ask("emptyimage[sw]") if not picture_txt_ref: picture_txt_ref = obj.ask("image[sw]") if not picture_txt_ref: picture_txt_ref = obj.ask("image[ne]") if not picture_txt_ref: raise Exception, "no images for object %s!" % objname # attempt at extracting the "best" image picture_ref = simutools.SimutransImgParam(picture_txt_ref) picfilename = os.path.join(os.path.dirname(obj.srcfile), picture_ref.file + ".png") big_pic = pygame.image.load(picfilename) srccoords = pygame.Rect(picture_ref.coords[1] * paksize, picture_ref.coords[0] * paksize, paksize, paksize) targetcoords = (0, 0, paksize, paksize) small_pic = pygame.Surface((paksize, paksize)) small_pic.blit(big_pic, targetcoords, srccoords) small_pic.set_colorkey((231, 255, 255)) srccoords = small_pic.get_bounding_rect() final_pic = pygame.Surface((srccoords.width, srccoords.height)) final_pic.fill((231, 255, 255)) final_pic.blit(small_pic, (0, 0), srccoords) final_pic.set_colorkey((231, 255, 255)) del big_pic, small_pic # final_pic is now only wanted picture targetcoords = final_pic.get_rect() targetcoords.bottom = start_y + item_height targetcoords.right = left_margin - 2 if is_disabled else start_x - 2 graphic_left = targetcoords.left canvas.blit(final_pic, targetcoords) del final_pic # picture is on the main canvas surf = font.render(objname, font_aa, name_color) targetcoords = surf.get_rect() targetcoords.top = start_y + 2 targetcoords.right = graphic_left - 4 canvas.blit(surf, targetcoords) # name written text = "%i km/h" % int(obj.ask("speed", 0)) pwr = int(obj.ask("power", 0)) if pwr > 0: text += ", %i kW" % pwr surf = font.render(text, font_aa, name_color) if not is_disabled: canvas.blit(surf, (start_x + 4, start_y + 2)) else: canvas.blit(surf, (left_margin + 4, start_y + 2)) return
def procObj(obj) : objname = obj.ask("name") objkind = obj.ask("obj") cname = simutools.canonicalObjName(objname) if use_obj_kind_prefix : cname = "%s,%s" % (cname, objkind) print("processing", objname) images = [] for param in simutools.ImageParameterNames : images += map(lambda i: (param, i[0], i[1]), obj.ask_indexed(param)) for param in simutools.SingleImageParameterNames : value = obj.ask(param) if value : images.append((param, "", value)) total_length_px = len(images) * paksize max_width_px = 1024.0 min_width_px = 384.0 # both fixed, based on common screen and text new_width_px = max(min(total_length_px, max_width_px), min_width_px) # restrict to range new_width = int(float(new_width_px) / paksize) new_height = math.ceil(len(images) / float(new_width)) + 1 newimage = pygame.Surface((paksize * new_width, paksize * new_height)) newimage.fill((231,255,255)) # background newimage.fill((255,255,255), (0, 0, new_width * paksize, paksize)) # description stripe text = "name: " + objname surf = font.render(text, False, (0,0,0), (255,255,255)) # black text on white newimage.blit(surf, (10, 10)) text = "copyright: " + obj.ask("copyright", "?", False) surf = font.render(text, False, (0,0,0), (255,255,255)) newimage.blit(surf, (10, 10 + fntsize + 4)) type = obj.ask("type", "?") if type != "?" : text = "obj: %s ; type: %s" % (objkind, type) else : text = "obj: %s" % objkind surf = font.render(text, False, (0,0,0), (255,255,255)) newimage.blit(surf, (10, 10 + 2 * (fntsize + 4))) text = "timeline: %s - %s" % (obj.ask("intro_year", "*"), obj.ask("retire_year", "*")) surf = font.render(text, False, (0,0,0), (255,255,255)) newimage.blit(surf, (10, 10 + 3 * (fntsize + 4))) targetcoords = [0, 1] for i in range(len(images)) : kind, indices, imgref = images[i] imgref = simutools.SimutransImgParam(imgref) if not imgref.isEmpty() : imgname = os.path.normpath(os.path.join(os.path.dirname(obj.srcfile), imgref.file + ".png")) if not imgname in loadedimages : loadedimages[imgname] = pygame.image.load(imgname) srccoords_px = pygame.Rect(imgref.coords[1] * paksize, \ imgref.coords[0] * paksize, paksize, paksize) # calculate position on old image imgref.coords[0] = targetcoords[1]; imgref.coords[1] = targetcoords[0] targetcoords_px = [x * paksize for x in targetcoords] newimage.blit(loadedimages[imgname], targetcoords_px, srccoords_px) # copy image tile imgref.file = cname obj.put(kind + indices, str(imgref)) targetcoords[0] += 1 if targetcoords[0] >= new_width : targetcoords[0] = 0 targetcoords[1] += 1 pygame.image.save(newimage, os.path.join(outdir, cname + ".png")) f = open(os.path.join(outdir, cname + ".dat"), 'w') for l in obj.lines : f.write(l) f.close()
def procObj(obj): objname = obj.ask("name") cname = simutools.canonicalObjName(objname) images = [] for param in simutools.ImageParameterNames: images += map(lambda i: (param, i[0], i[1]), obj.ask_indexed(param)) for param in simutools.SingleImageParameterNames: value = obj.ask(param) if value: images.append((param, "", value)) newimage = pygame.Surface((paksize, paksize)) newimage.fill((231, 255, 255)) # background for i in range(len(images)): key, indices, raw_ref = images[i] imgref = simutools.SimutransImgParam(raw_ref) if not imgref.isEmpty(): imgname = os.path.normpath( os.path.join(os.path.dirname(obj.srcfile), imgref.file + ".png")) if not imgname in loadedimages: loadedimages[imgname] = pygame.image.load(imgname) srccoords_px = pygame.Rect(imgref.coords[1] * paksize, \ imgref.coords[0] * paksize, paksize, paksize) # calculate position on old image newimage.blit(loadedimages[imgname], (0, 0), srccoords_px) # copy image tile off_x, off_y = imgref.offset off_x, off_y = -off_x, -off_y changed = True save = False while True: pygame.time.wait(50) pygame.event.pump() keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] or keys[pygame.K_ESCAPE]: pygame.time.wait(500) break elif keys[pygame.K_RETURN]: pygame.time.wait(500) save = True break elif keys[pygame.K_LEFT]: off_x = off_x - 1 changed = True elif keys[pygame.K_RIGHT]: off_x = off_x + 1 changed = True elif keys[pygame.K_UP]: off_y = off_y - 1 changed = True elif keys[pygame.K_DOWN]: off_y = off_y + 1 changed = True elif pygame.mouse.get_pressed()[0]: mx, my = pygame.mouse.get_pos() off_x = mx - 100 - paksize / 2 off_y = my - 100 - (paksize * 3) / 4 changed = True if changed: screen.fill((0, 0, 0)) screen.blit(newimage, (100, 100)) screen.blit(cursor, (100 + off_x, 100 + off_y), newimage.get_bounding_rect()) pygame.display.flip() changed = False if save: imgref.offset = -off_x, -off_y obj.put(key + indices, str(imgref)) f = open(os.path.join(outdir, cname + ".dat"), 'w') for l in obj.lines: f.write(l) f.close()
def procObj(obj) : objname = obj.ask("name") print("processing", objname) if obj.ask("type", "none").lower() in ["res", "com", "ind", "cur"] : # needs default string because of lower() images = obj.ask_indexed("backimage") total_length_px = len(images) * paksize max_width_px = 1024.0 min_width_px = 384.0 # both fixed, based on common screen and text new_width_px = max(min(total_length_px, max_width_px), min_width_px) # restrict to range new_width = int(float(new_width_px) / paksize) new_height = math.ceil(len(images) / float(new_width)) + 1 newimage = pygame.Surface((paksize * new_width, paksize * new_height)) newimage.fill((231,255,255)) # background newimage.fill((255,255,255), (0, 0, new_width * paksize, paksize)) # description stripe text = "name: " + obj.ask("name", "?") surf = font.render(text, False, (0,0,0), (255,255,255)) # black text on white newimage.blit(surf, (10, 10)) text = "copyright: " + obj.ask("copyright", "?") surf = font.render(text, False, (0,0,0), (255,255,255)) # black text on white newimage.blit(surf, (10, 10 + fntsize + 4)) cname = simutools.canonicalObjName(objname) targetcoords = [0, 1] for i in range(len(images)) : indices, imgref = images[i] imgref = simutools.SimutransImgParam(imgref) imgname = os.path.normpath(os.path.join(os.path.dirname(obj.srcfile), imgref.file + ".png")) if not imgname in loadedimages : loadedimages[imgname] = pygame.image.load(imgname) srccoords_px = pygame.Rect(imgref.coords[1] * paksize, \ imgref.coords[0] * paksize, paksize, paksize) # calculate position on old image imgref.coords[0] = targetcoords[1]; imgref.coords[1] = targetcoords[0] targetcoords_px = [x * paksize for x in targetcoords] newimage.blit(loadedimages[imgname], targetcoords_px, srccoords_px) # copy image tile imgref.file = cname obj.put("backimage" + indices, str(imgref)) targetcoords[0] += 1 if targetcoords[0] >= new_width : targetcoords[0] = 0 targetcoords[1] += 1 pygame.image.save(newimage, os.path.join(outdir, cname + ".png")) f = open(os.path.join(outdir, cname + ".dat"), 'w') for l in obj.lines : f.write(l) f.close()
def procObj(i): global canvas obj = Data[i] objname = obj.ask("name") row_number = i / items_per_row # which row is this in row_position = i % items_per_row # which item is this in that row picture_txt_ref = None if not picture_txt_ref: picture_txt_ref = obj.ask("image[3][0]") if not picture_txt_ref: picture_txt_ref = obj.ask("image[0][0]") if not picture_txt_ref: raise Exception, "no images for object %s!" % objname # attempt at extracting the "best" image picture_ref = simutools.SimutransImgParam(picture_txt_ref) picfilename = os.path.join(os.path.dirname(obj.srcfile), picture_ref.file + ".png") big_pic = pygame.image.load(picfilename) srccoords = pygame.Rect(picture_ref.coords[1] * paksize, picture_ref.coords[0] * paksize, paksize, paksize) targetcoords = (0, 0, paksize, paksize) small_pic = pygame.Surface((paksize, paksize)) small_pic.blit(big_pic, targetcoords, srccoords) small_pic.set_colorkey((231, 255, 255)) srccoords = small_pic.get_bounding_rect() final_pic = pygame.Surface((srccoords.width, srccoords.height)) final_pic.fill((231, 255, 255)) final_pic.blit(small_pic, (0, 0), srccoords) final_pic.set_colorkey((231, 255, 255)) del big_pic, small_pic # final_pic is now only wanted picture targetcoords = final_pic.get_rect() targetcoords.bottom = vertical_margin + item_height * row_number + tree_baseline targetcoords.left = side_margin + item_width * row_position canvas.blit(final_pic, targetcoords) del final_pic # picture is on the main canvas surf = font.render(objname, font_aa, name_color) targetcoords = surf.get_rect() targetcoords.top = vertical_margin + item_height * row_number + tree_baseline + 2 targetcoords.left = side_margin + item_width * row_position + 4 canvas.blit(surf, targetcoords) # name written climates = obj.ask("climates", "").split(",") for j, name in enumerate(climate_names): place = (targetcoords.left + j * (climate_size - 1) - 1, targetcoords.top + 12, climate_size, climate_size) if name in climates: canvas.fill(climate_colors[name], place) pygame.draw.rect(canvas, name_color, place, 1) return