def draw(): clear_canvas() image_black.draw(app.width / 2 * app.scale, app.height / 2 * app.scale, app.width * app.scale, app.height * app.scale) font.draw('PRESS ANY KEY TO ENTER LOBBY', app.width / 2, app.height / 2, font.WHITE, font.MIDDLE) update_canvas()
def fittext(self, rect, text, wordwrap=False, fill='none'): surface = self.lcd.surface bw = self.lcd.bw #print('fittext', text, wordwrap, fill) if fill != 'none': surface.box(*(self.convrect(rect) + [fill])) metric_size = 16 ptext = text spaces = ' ' in text # numbers have same width if spaces: ntext = text else: ntext = '' for c in text: if c.isdigit(): ntext += '0' else: ntext += c for t in self.fittext_cache: if t[0] == ntext: t0, size, r, ptext = t self.fittext_cache.remove(t) if r.width == rect.width and r.height == rect.height: self.fittext_cache.append(t) break else: if wordwrap: size, ptext = self.fittextsizewordwrap(rect, text, metric_size, bw, surface) else: s = font.draw(surface, False, text, metric_size, bw) if s[0] == 0 or s[1] == 0: return 0, 0 sw = surface.width * float(rect.width) / s[0] sh = surface.height * float(rect.height) / s[1] size = int(min(sw * metric_size, sh * metric_size)) try: self.lcd.client.reset_timeout() except: pass #time.sleep(.02) # this line is required! needed to process wifi packets durning long sleep if wordwrap: # only cache wordwrap fit!! #print('fittext cache len', ntext, len(self.fittext_cache), list(map(lambda x : x[0], self.fittext_cache))) self.fittext_cache.append( (ntext, size, rect, ptext if spaces else None)) if len(self.fittext_cache) > 8: self.fittext_cache = self.fittext_cache[1:] pos = int(rect.x * surface.width), int(rect.y * surface.height) if not ptext: ptext = text size = font.draw(surface, pos, ptext, size, bw) return float(size[0]) / surface.width, float(size[1]) / surface.height
def draw(): clear_canvas() image_black.draw(app.width / 2 * app.scale, app.height / 2 * app.scale, app.width * app.scale, app.height * app.scale) if state is TIMER: font.draw('WAIT A MAMENT ' + str(int(6 - (get_time() - intro_time))), app.width / 2, app.height / 2 + 5, font.WHITE, font.MIDDLE) elif state is TAITO: image_TAITO.draw(app.width / 2 * app.scale, app.height / 2 * app.scale, image_TAITO.w * app.scale, image_TAITO.h * app.scale) update_canvas()
def text(self, pos, text, size, crop=False): pos = int(pos[0] * self.surface.width), int(pos[1] * self.surface.height) size = int(size * self.surface.width / 48) size = font.draw(self.surface, pos, text, size, self.bw, crop) return float(size[0]) / self.surface.width, float( size[1]) / self.surface.height
def draw(): clear_canvas() image_back.draw(app.width / 2 * app.scale, app.height / 2 * app.scale, app.width * app.scale, app.height * app.scale) font.draw('PUSH OR SELECT BUTTON', app.width / 2, app.height / 2 + 4, font.WHITE, font.MIDDLE) image_player.clip_draw(0, 0, image_player.w // 2, image_player.h, (app.width / 2 - 68) * app.scale, (app.height / 2 + 4) * app.scale, image_player.w // 2 * app.scale, image_player.h * app.scale) image_player.clip_draw(image_player.w // 2, 0, image_player.w // 2, image_player.h, (app.width / 2 - 12) * app.scale, (app.height / 2 + 4) * app.scale, image_player.w // 2 * app.scale, image_player.h * app.scale) update_canvas()
def draw(self): refresh2d(1, 1, self.window_size.x, self.window_size.y) # set mode to 2d #glClear(GL_COLOR_BUFFER_BIT) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, tex.g_texture) for loc in self.map: stex = "unit" if loc in self.units else "bound" rot = 0 if self.path == None or not loc in self.path: # tile is not in range of selected unit if self.mouseloc != None and loc == self.mouseloc: c = color.d_color["BLUE"] else: c = color.d_color["WHITE"] elif self.loc_has_unit(loc) and (self.selected == None or loc != self.selected): c = color.d_color["WHITE"] else: c = color.d_color["RED"] weightmult = ((self.map[loc].weight - RANDRANGE[0] + 1.0) / (RANDRANGE[1] - RANDRANGE[0] + 1.0)) #weightmult for an appropriate RANDRANGE should be [W+1/dR+1 for W] -> [1/2,2/2] for dR=1 c = c * weightmult c.draw() tile.draw_square(loc, self.size, tex.get_texcoords(stex, rot), rot) if self.pathtex and loc in self.pathtex: itex, rot = self.pathtex[loc] (color.d_color["WHITE"] * 0.5).draw() tile.draw_square(loc, self.size, tex.get_texcoords(g_texnames[itex], rot), rot) glDisable(GL_TEXTURE_2D) if self.tooltip.do_render and self.wmouseloc != None: s = "data: " + repr(self.tooltip.data) font.draw(s, self.wmouseloc[0], self.wmouseloc[1] + 3.0 / self.window_size.y, True, self.window_size, self.size) if SHOWFPS: self.myfps.update() self.myfps.draw((0.0, 0.0), self.window_size, self.size) glutSwapBuffers()
def fittext(self, rect, text, wordwrap=False, fill='none'): surface = self.lcd.surface bw = self.lcd.bw #print('fittext', text, wordwrap, fill) if fill != 'none': surface.box(*(self.convrect(rect) + [fill])) metric_size = 16 ptext = text if text in self.fittext_cache: size, r, ptext = self.fittext_cache[text] if r.width != rect.width or r.height != rect.height: del self.fittext_cache[text] if not text in self.fittext_cache: if wordwrap: size, ptext = self.fittextsizewordwrap(rect, text, metric_size, bw, surface) else: s = font.draw(surface, False, text, metric_size, bw) if s[0] == 0 or s[1] == 0: return 0, 0 sw = surface.width * float(rect.width) / s[0] sh = surface.height * float(rect.height) / s[1] size = int(min(sw * metric_size, sh * metric_size)) try: self.lcd.client.reset_timeout() except: pass #time.sleep(.02) # this line is required! needed to process wifi packets durning long sleep if wordwrap: # only cache wordwrap fit!! self.fittext_cache[text] = size, rect, ptext pos = int(rect.x * surface.width), int(rect.y * surface.height) size = font.draw(surface, pos, ptext, size, bw) return float(size[0]) / surface.width, float(size[1]) / surface.height
def draw(): clear_canvas() image_back.draw(app.width / 2 * app.scale, app.height / 2 * app.scale, app.width * app.scale, app.height * app.scale) font.draw(' THE NEW ADVENTURE OF', app.width / 2, 200, font.GREEN, 0.5) font.draw('@BUBBLE BOBBLE\"', app.width / 2, 200 - 16, font.GREEN, 0.5) font.draw('- THE NEXT GENERATION -', app.width / 2, 200 - 32, font.GREEN, 0.5) font.draw_to_origin('LET^ TRY AND CHALLENGE!', app.width / 2, 40, font.WHITE, 0.5) update_canvas()
def draw(self): if self.stage is 1: self.x = (self.x + 10 * app.elapsed_time) % 512 self.image.clip_draw_to_origin(760, 0, 512, 112, -self.x * app.scale, (app.height - 112) * app.scale, 512 * app.scale, 112 * app.scale) self.image.clip_draw_to_origin(760, 0, 512, 112, (-self.x + 512) * app.scale, (app.height - 112) * app.scale, 512 * app.scale, 112 * app.scale) self.image.clip_draw_to_origin(440, 0, 320, 219, 0, 0, 320 * app.scale, 219 * app.scale) elif 2 <= self.stage <= 7: self.frame = (self.frame + 7 * app.elapsed_time) % 3 self.image.clip_draw(440, 219, 352, 224, app.width / 2 * app.scale, app.height / 2 * app.scale, 352 * app.scale, 224 * app.scale) self.image.clip_draw(792 + 320 * int(self.frame), 219, 320, 188, app.width / 2 * app.scale, (app.height / 2 - 18) * app.scale, 320 * app.scale, 188 * app.scale) self.image.clip_draw_to_origin(0, (self.stage - 1) * 208, 320, 208, 0, 0, 320 * app.scale, 208 * app.scale) self.image.clip_draw_to_origin(760 + 5, 112, 1, 1, 0, (app.height - 16) * app.scale, app.width * app.scale, app.height * app.scale) font.draw('HIGH SCORE', app.width / 2 + 4, app.height - 4, font.PINK, 0.5) font.draw(str(app.best_score), app.width / 2 + 44, app.height - 12, font.WHITE, 1) font.draw('1UP', app.width / 2 - 88, app.height - 4, font.GREEN, 0.5) font.draw(str(app.score), app.width / 2 - 60, app.height - 12, font.WHITE, 1)
def fittextsizewordwrap(self, rect, text, metric_size, bw, surface): t0 = time.time() words = text.split(' ') if not words or not words[0]: return 0, '' spacewidth = font.draw(surface, False, ' ', metric_size, self.lcd.bw)[0] metrics = [] for word in words: metrics.append( (word, font.draw(surface, False, word, metric_size, bw))) # needed in micropython because this routine can be so slow the recieve buffer can overflow self.lcd.receive() t1 = time.time() widths = list(map(lambda metric: metric[1][0], metrics)) maxwordwidth = max(*widths + [0]) totalwidth = sum(widths) + spacewidth * (len(words) - 1) t2 = time.time() size = 0 # calculate where to wrap words to maximize font size wrappos = maxwordwidth while True: posx, posy = 0, 0 curtext = '' lineheight = 0 maxw = 0 minfirstwidth = maxwordwidth for metric in metrics: word, (width, height) = metric if posx > 0: width += spacewidth if posx + width > wrappos: curtext += '\n' posx = 0 posy += lineheight if width < minfirstwidth: minfirstwidth = width lineheight = 0 if posx > 0: curtext += ' ' curtext += word lineheight = max(lineheight, height) posx += width maxw = max(maxw, posx) maxh = posy + lineheight s = maxw, maxh if s[0] == 0 or s[1] == 0: continue sw = surface.width * float(rect.width) / s[0] sh = surface.height * float(rect.height) / s[1] cursize = int(min(sw * metric_size, sh * metric_size)) if cursize < size: break size = cursize text = curtext if posy == 0: break wrappos += minfirstwidth t3 = time.time() return size, text
import ugfx import glut white = ugfx.color(255, 255, 255) black = ugfx.color(0, 0, 0) use_glut = 'DISPLAY' in os.environ if use_glut: screen = glut.screen() else: screen = ugfx.screen("/dev/fb0") c, d = black, white x, w = 0, 400 while w >= 100: screen.box(x, x, x+w, x+w, c) x += 2 w -= 4 c, d = d, c screen.invert(100, 100, 200, 200) import font font.draw(screen, (0, 0), "Hello!", 80, False) print 'type', str(type(screen)) if use_glut: from OpenGL.GLUT import glutMainLoop glutMainLoop()
def fittext(self, rect, text, wordwrap=False, fill='none'): #print 'fittext', text, wordwrap, fill if fill != 'none': self.surface.box(*(self.convrect(rect) + [fill])) metric_size = 16 if wordwrap: words = text.split(' ') spacewidth = font.draw(self.surface, False, ' ', metric_size, self.bw)[0] if len(words) < 2: # need at least 2 words to wrap return self.fittext(rect, text, False, fill) metrics = map(lambda word : (word, font.draw(self.surface, False, word, metric_size, self.bw)), words) widths = map(lambda metric : metric[1][0], metrics) maxwordwidth = apply(max, widths) totalwidth = sum(widths) + spacewidth * (len(words) - 1) size = 0 # not very efficient... just tries each x position # for wrapping to maximize final font size for wrappos in range(maxwordwidth, totalwidth+1): posx, posy = 0, 0 curtext = '' lineheight = 0 maxw = 0 for metric in metrics: word, (width, height) = metric if posx > 0: width += spacewidth if posx + width > wrappos: curtext += '\n' posx = 0 posy += lineheight lineheight = 0 if posx > 0: curtext += ' ' curtext += word lineheight = max(lineheight, height) posx += width maxw = max(maxw, posx) maxh = posy + lineheight s = maxw, maxh if s[0] == 0 or s[1] == 0: continue sw = self.surface.width * float(rect.width) / s[0] sh = self.surface.height * float(rect.height) / s[1] cursize = int(min(sw*metric_size, sh*metric_size)) if cursize > size: size = cursize text = curtext else: s = font.draw(self.surface, False, text, metric_size, self.bw) if s[0] == 0 or s[1] == 0: return 0, 0 sw = self.surface.width * float(rect.width) / s[0] sh = self.surface.height * float(rect.height) / s[1] size = int(min(sw*metric_size, sh*metric_size)) pos = int(rect.x*self.surface.width), int(rect.y*self.surface.height) size = font.draw(self.surface, pos, text, size, self.bw) return float(size[0])/self.surface.width, float(size[1])/self.surface.height
def draw(): clear_canvas() image_back.draw(app.width / 2 * app.scale, app.height / 2 * app.scale, app.width * app.scale, app.height * app.scale) font.draw('RANK', app.width / 4 * 1 + 4, app.height / 4 * 3, font.WHITE, font.MIDDLE) font.draw('SCORE', app.width / 4 * 2 - 16, app.height / 4 * 3, font.WHITE, font.MIDDLE) font.draw('PLAY TIME', app.width / 4 * 3 - 16, app.height / 4 * 3, font.WHITE, font.MIDDLE) for i in range(0, app.ranking.__len__()): if app.ranking[i][0] == app.score and app.ranking[i][1] == app.play_time: color = font.RED else: color = font.WHITE font.draw(str(i + 1), app.width / 4 * 1 + 20, app.height / 4 * 3 - (i + 2) * 12, color, font.RIGHT) font.draw(str(app.ranking[i][0]), app.width / 4 * 2 + 4, app.height / 4 * 3 - (i + 2) * 12, color, 1) font.draw(str(int(app.ranking[i][1] * 100) / 100) + ' S', app.width / 4 * 3 + 20, app.height / 4 * 3 - (i + 2) * 12, color, 1) # font.draw(get_canvas_width() // 2 - 80, get_canvas_height() // 2 + 100 - 20 * i, # "#" + str(i+1) + ". " + '%.2f' % ranking[i]) update_canvas()
def draw(self, position, window_size, size): delta = self.mytime[0]-self.mytime[1] rate = 1000000.0/delta.microseconds s = "freq: " + repr(rate) font.draw(s, position[0], position[1], True, window_size, size)