# head up display - draws a semi-transparent text box. class Hud(object): def __init__(self): self.app = [] self.strs = [] self.fontbase = [] self.hud_lines = [] self.show_tags = [] self.hide_alls = [] self.hud_rgba_r = 0 self.hud_rgba_g = 0.2 self.hud_rgba_b = 0 self.hud_rgba_a = 0.5 self.hud_text_rgb_r = 1 self.hud_text_rgb_g = 0.8 self.hud_text_rgb_b = 0.4 # legacy vismach models used this def show(self, string): self.add_txt(string) # displays a string, optionally a tag or list of tags can be assigned def add_txt(self, string, tag=None): self.hud_lines += [[str(string), None, tag]] # displays a formatted pin value (can be embedded in a string) def add_pin(self, string, pin=None, tag=None): self.hud_lines += [[str(string), pin, tag]] # shows all lines with the specified tag if the pin value = val def show_tag_if_same(self, tag, pin, val=True): self.show_tags += [[tag, pin, val]] # shows all lines with a tag equal to the pin value + offset def show_tags_in_pin(self, pin, offs=0): self.show_tags += [[pin, None, offs]] # hides the complete hud if the pin value is equal to val def hide_all(self, pin, val=True): self.hide_alls += [[pin, val]] # changes the hud color and transparency def set_hud_rgba(self, r, g, b, a): self.hud_rgba_r = r self.hud_rgba_g = g self.hud_rgba_b = b self.hud_rgba_a = a # changes the hud text color def set_hud_text_rgb(self, r, g, b): self.hud_text_rgb_r = r self.hud_text_rgb_g = g self.hud_text_rgb_b = b # update the lines in the hud using the lists created above def draw(self): hide_hud = 0 strs = [] show_list = [None] # check if hud should be hidden for a in self.hide_alls: if hal.get_value(a[0]) == a[1]: hide_hud = 1 if hide_hud == 0: # create list of all line tags to be shown for b in self.show_tags: if b[1] == None: # _show_tags_in_pin tag = int(hal.get_value(b[0]) + b[2]) else: # _show_tag_if_same if hal.get_value(b[1]) == b[2]: tag = b[0] if not isinstance(tag, list): tag = [tag] show_list = show_list + tag # build the for c in self.hud_lines: if not isinstance(c[2], list): c[2] = [c[2]] if any(item in c[2] for item in show_list): if c[1] == None: # _txt strs += [c[0]] else: # _pin strs += [c[0].format(hal.get_value(c[1]))] drawtext = strs # draw head-up-display # see axis.py for more font/color configurability if len(drawtext) == 0: return glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() if not self.fontbase: self.fontbase = int(self.app.loadbitmapfont("9x15")) char_width, char_height = 9, 15 xmargin,ymargin = 5,5 ypos = float(self.app.winfo_height()) glOrtho(0.0, self.app.winfo_width(), 0.0, ypos, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() # draw the text box maxlen = max([len(p) for p in drawtext]) box_width = maxlen * char_width glDepthFunc(GL_ALWAYS) glDepthMask(GL_FALSE) glDisable(GL_LIGHTING) glEnable(GL_BLEND) glEnable(GL_NORMALIZE) glBlendFunc(GL_ONE, GL_CONSTANT_ALPHA) # sets the color of the hud overlay glColor3f(self.hud_rgba_r, self.hud_rgba_g, self.hud_rgba_b) # rgba, sets the transparency of the overlay using the 'a' value glBlendColor(0,0,0,self.hud_rgba_a) glBegin(GL_QUADS) # upper left glVertex3f(0, ypos, 1) # lower left glVertex3f(0, ypos - 2*ymargin - char_height*len(drawtext), 1) # lower right glVertex3f(box_width+2*xmargin, ypos - 2*ymargin - char_height*len(drawtext), 1) # upper right glVertex3f(box_width+2*xmargin, ypos , 1) glEnd() glDisable(GL_BLEND) glEnable(GL_LIGHTING) # fill the box with text maxlen = 0 ypos -= char_height+ymargin i = 0 glDisable(GL_LIGHTING) # sets the color of the text in the hud glColor3f(self.hud_text_rgb_r, self.hud_text_rgb_g, self.hud_text_rgb_b) for string in drawtext: maxlen = max(maxlen, len(string)) glRasterPos2i(xmargin, int(ypos)) for char in string: glCallList(self.fontbase + ord(char)) ypos -= char_height i = i + 1 glDepthFunc(GL_LESS) glDepthMask(GL_TRUE) glEnable(GL_LIGHTING) glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW)