def draw(self, ctx): if self.__highlighted: bg_color = nanogui.Color(0.95, 0.95, 0.95, 1.0) frame_color = nanogui.Color(0.0, 0.0, 0.0, 1.0) else: bg_color = nanogui.Color(0.95, 0.95, 0.95, 0.6) frame_color = nanogui.Color(0.0, 0.0, 0.0, 0.6) x, y = self.position() w, h = self.size() ctx.BeginPath() ctx.RoundedRect(x + 1, y + 1, w - 2, h - 2, 5) ctx.FillColor(bg_color) ctx.Fill() super().draw(ctx) ctx.BeginPath() ctx.RoundedRect(x + 1, y + 1, w - 2, h - 2, 5) ctx.StrokeWidth(2.0) ctx.StrokeColor(frame_color) ctx.Stroke()
def lighten_cb(): curr = self.color() r = curr.r + (0.05 * (1.0 - curr.r)) g = curr.g + (0.05 * (1.0 - curr.g)) b = curr.b + (0.05 * (1.0 - curr.b)) next_color = nanogui.Color(r, g, b, curr.w) self.master_callback(next_color) for child in self.popup().children(): if isinstance(child, nanogui.Button) and child.caption() == "Pick": child.setBackgroundColor(next_color) child.setTextColor(next_color.contrastingColor()) elif isinstance(child, nanogui.ColorWheel): child.setColor(next_color)
def __init__(self, title): super(MainScreen, self).__init__( (800, 600), # window size title, # window title True, # resizable (boolean) False # fullscreen (boolean) ) # initialize handlers self.screen_draw_handler = lambda: None self.screen_resize_handler = lambda size: None self.mouse_motion_handler = lambda pos, rel, button, modifiers: None self.keyboard_handler = lambda key, scancode, action, modifiers: None # screen properties self.setBackground(ng.Color(0.1, 0.1, 0.1, 1.0))
def loadFromJSON(self, values): sizes = [ "mStandardFontSize", "mButtonFontSize", "mTextBoxFontSize", "mWindowFontSize", "mWindowCornerRadius", #"mIconScale" "mWindowHeaderHeight", "mWindowDropShadowSize", "mButtonCornerRadius", "mTabBorderWidth", "mTabInnerMargin", "mTabMinButtonWidth", "mTabMaxButtonWidth", "mTabControlWidth", "mTabButtonHorizontalPadding", "mTabButtonVerticalPadding", "mTooltipOpacity" # not a size, but is a float so do here ] for s in sizes: if s in values: val = values[s] self.__setattr__(s, val) self.number_pickers[s].setValue(val) else: raise RuntimeError("The key '{0}' was not provided.".format(s)) colors = [ "mDropShadow", "mTransparent", "mBorderDark", "mBorderLight", "mBorderMedium", "mTextColor", "mDisabledTextColor", "mTextColorShadow", "mIconColor", "mButtonGradientTopFocused", "mButtonGradientBotFocused", "mButtonGradientTopUnfocused", "mButtonGradientBotUnfocused", "mButtonGradientTopPushed", "mButtonGradientBotPushed", "mTooltipBackgroundColor", "mTooltipTextColor", "mWindowFillUnfocused", "mWindowFillFocused", "mWindowTitleUnfocused", "mWindowTitleFocused", "mWindowHeaderGradientTop", "mWindowHeaderGradientBot", "mWindowHeaderSepTop", "mWindowHeaderSepBot", "mWindowPopup", "mWindowPopupTransparent" ] for c in colors: if c in values: r, g, b, a = values[c] col = nanogui.Color(r, g, b, a) self.__setattr__(c, col) self.color_pickers[c].reset(col) else: raise RuntimeError("The key '{0}' was not provided.".format(s))
def img_view_cb(index): stringData = "" channelSum = 0.0 pixel = screen.imageData[index[1], index[0]] for i in range(4): channelData = pixel[i] channelSum += float(channelData) stringData += "{0}\n".format(channelData) intensity = (255.0 - (channelSum / 4.0)) / 255.0 if intensity > 0.5: colorScale = (intensity + 1.0) / 2.0 else: colorScale = intensity / 2.0 textColor = nanogui.Color(colorScale, 1.0) return (stringData, textColor)
def __init__(self, parent, title): super().__init__(parent) layout = nanogui.AdvancedGridLayout([0], [0, 0]) layout.setMargin(4) layout.setColStretch(0, 1.0) layout.setRowStretch(1, 1.0) self.setLayout(layout) titlebar = nanogui.Label(self, title) titlebar.setColor(nanogui.Color(0.0, 0.0, 0.0, 1.0)) layout.setAnchor(titlebar, nanogui.AdvancedGridLayout.Anchor(0, 0)) body = nanogui.Widget(self) layout.setAnchor(body, nanogui.AdvancedGridLayout.Anchor(0, 1)) nanogui.CheckBox(body, "A check box") self.__highlighted = False self.__drag_handle_pos = None
def draw(self, ctx): ctx.Save() try: ctx.Translate(*self.position()) ctx.Scale(self.__zoom, self.__zoom) for n1, x1, y1, n2, x2, y2 in self.__connections: p1 = n1.position() + (x1, y1) p2 = n2.position() + (x2, y2) cpos = (min(100, abs(p2[0] - p1[0]) / 2), 0) ctx.BeginPath() ctx.StrokeWidth(2.0) ctx.StrokeColor(nanogui.Color(0.0, 0.0, 0.0, 1.0)) ctx.MoveTo(*p1) ctx.BezierTo(*(p1 + cpos), *(p2 - cpos), *p2) ctx.Stroke() for node in self.__nodes: node.draw(ctx) finally: ctx.Restore()
# # All rights reserved. Use of this source code is governed by a # BSD-style license that can be found in the LICENSE.txt file. import nanogui import math import gc from nanogui import Screen, FormHelper, Vector2i bvar = True ivar = 12345678 dvar = math.pi strvar = "A string" enumvar = 1 colvar = nanogui.Color(.5, .5, .7, 1) def make_accessors(name): def setter(value): globals()[name] = value def getter(): return globals()[name] return setter, getter nanogui.init() use_gl_4_1 = False # Set to True to create an OpenGL 4.1 context.
def makeCompareWindow(screen, title, themeChoice): window = nanogui.Window(screen, title) # By setting the theme now *BEFORE* any children are added, this means that # all new children created will inherit a reference to this custom theme. # # When you call setTheme after-the-fact, the same will occur -- calling # setTheme on a widget propagates that theme to all children. if isinstance(themeChoice, nanogui.Theme): # theme builder bypass window.setTheme(themeChoice) else: if themeChoice == ThemeChoice.Custom: window.setTheme(screen.customTheme) elif themeChoice == ThemeChoice.Fontawesome: window.setTheme(screen.fontawesomeTheme) # The remainder of the code here is largely irrelevant, the setTheme is the # important part. Everything added below exists as a testing suite to make # sure that all widgets that draw fonts are obeying the new theme's default # font selections as defined by CustomTheme::defaultFont and # CustomTheme::defaultBoldFont overrides (see custom_theme.py). window.setLayout(nanogui.GroupLayout(15, 6, 6)) # test text box fonts nanogui.Label(window, "Text Boxes") wrapper = nanogui.Widget(window) grid_layout = nanogui.GridLayout() grid_layout.setColAlignment( [nanogui.Alignment.Maximum, nanogui.Alignment.Fill]) wrapper.setLayout(grid_layout) nanogui.Label(wrapper, "TextBox : ") nanogui.TextBox(wrapper, "Some Text").setEditable(True) nanogui.Label(wrapper, "IntBox : ") nanogui.IntBox(wrapper).setSpinnable(True) nanogui.Label(wrapper, "FloatBox : ") nanogui.FloatBox(wrapper).setSpinnable(True) # test ImageView fonts (needs callback, scroll up on image to see) if not isinstance(themeChoice, nanogui.Theme): nanogui.Label(window, "Image View (Mouse Over Image and Scroll Up)") # NOTE: screen.icons loaded in constructor! On Linux the ordering is # not alphanumeric (it seems to be by image size?), hence the loop imageView = None for identifier, basename in screen.icons: if "icon1" in basename: imageView = nanogui.ImageView(window, identifier) imageView.setGridThreshold(20) imageView.setPixelInfoThreshold(20) break if imageView is None: raise RuntimeError( "Critical error: icon1 not found in CustomScreen.icons!") def img_view_cb(index): stringData = "" channelSum = 0.0 pixel = screen.imageData[index[1], index[0]] for i in range(4): channelData = pixel[i] channelSum += float(channelData) stringData += "{0}\n".format(channelData) intensity = (255.0 - (channelSum / 4.0)) / 255.0 if intensity > 0.5: colorScale = (intensity + 1.0) / 2.0 else: colorScale = intensity / 2.0 textColor = nanogui.Color(colorScale, 1.0) return (stringData, textColor) imageView.setPixelInfoCallback(img_view_cb) # Message dialogs def message_cb(result): print("Dialog result: {0}".format(result)) nanogui.Label(window, "Message Dialogues") tools = nanogui.Widget(window) tools.setLayout( nanogui.BoxLayout(nanogui.Orientation.Horizontal, nanogui.Alignment.Middle, 0, 6)) dialogTheme = None if isinstance(themeChoice, nanogui.Theme): # theme builder bypass dialogTheme = themeChoice else: if themeChoice == ThemeChoice.Custom: dialogTheme = screen.customTheme elif themeChoice == ThemeChoice.Fontawesome: dialogTheme = screen.fontawesomeTheme def info_cb(): dlg = nanogui.MessageDialog( screen, nanogui.MessageDialog.Type.Information, "Title", "This is an information message", ) if dialogTheme: dlg.setTheme(dialogTheme) dlg.setCallback(message_cb) b = nanogui.Button(tools, "Info") b.setCallback(info_cb) def warn_cb(): dlg = nanogui.MessageDialog(screen, nanogui.MessageDialog.Type.Warning, "Title", "This is a warning message") if dialogTheme: dlg.setTheme(dialogTheme) dlg.setCallback(message_cb) b = nanogui.Button(tools, "Warn") b.setCallback(warn_cb) def ask_cb(): dlg = nanogui.MessageDialog(screen, nanogui.MessageDialog.Type.Question, "Title", "This is a question message", "Yes", "No", True) if dialogTheme: dlg.setTheme(dialogTheme) dlg.setCallback(message_cb) b = nanogui.Button(tools, "Ask") b.setCallback(ask_cb) # TabWidget used to test TabHeader and others while keeping the size manageable nanogui.Label(window, "Tab Widget") tabWidget = nanogui.TabWidget(window) # test button and checkbox fonts layer = tabWidget.createTab("Button Like") layer.setLayout(nanogui.GroupLayout()) # green color, produces white chevron at start cp = nanogui.ColorPicker(layer, nanogui.Color(0.28573, 0.56702, 0.25104, 1.0)) if isinstance(themeChoice, nanogui.Theme) or themeChoice != ThemeChoice.Default: cp.setSide(nanogui.Popup.Side.Left) def cp_cb(col): print("Color: {0}, {1}, {2}, {3}".format(col.r, col.g, col.b, col.w)) cp.setFinalCallback(cp_cb) # combobox cb = nanogui.ComboBox( layer, ["Combo box item 1", "Combo box item 2", "Combo box item 3"]) if isinstance(themeChoice, nanogui.Theme) or themeChoice != ThemeChoice.Default: cb.setSide(nanogui.Popup.Side.Left) icon = entypo.ICON_EXPORT if not isinstance( themeChoice, nanogui.Theme) and themeChoice == ThemeChoice.Fontawesome: icon = fontawesome.ICON_SOLID_SHARE_SQUARE # popup button popupBtn = nanogui.PopupButton(layer, "Popup", icon) popup = popupBtn.popup() # making sure the popup button for the custom theme stays in bounds if isinstance( themeChoice, nanogui.Theme ) or themeChoice == ThemeChoice.Custom or themeChoice == ThemeChoice.Fontawesome: popupBtn.setSide(nanogui.Popup.Side.Left) popup.setLayout(nanogui.GroupLayout()) nanogui.Label(popup, "Arbitrary widgets can be placed here") nanogui.CheckBox(popup, "A check box") # popup right icon = entypo.ICON_FLASH if themeChoice == ThemeChoice.Fontawesome: icon = fontawesome.ICON_SOLID_BOLT popupBtn = nanogui.PopupButton(popup, "Recursive popup", icon) popupRight = popupBtn.popup() popupRight.setLayout(nanogui.GroupLayout()) nanogui.CheckBox(popupRight, "Another check box") # popup left popupBtn = nanogui.PopupButton(popup, "Recursive popup", icon) popupBtn.setSide(nanogui.Popup.Side.Left) popupLeft = popupBtn.popup() popupLeft.setLayout(nanogui.GroupLayout()) nanogui.CheckBox(popupLeft, "Another check box") # regular buttons button = nanogui.Button(layer, "PushButton") # test that non-bold fonts for buttons work (applying to radio buttons) if isinstance(themeChoice, nanogui.Theme): radio_font = themeChoice.mDefaultFont else: # dynamic theme case, this will just be 'sans' radio_font = nanogui.Theme.GlobalDefaultFonts.Normal button = nanogui.Button(layer, "Radio1 (Hover for Tooltip)") button.setFont(radio_font) button.setFlags(nanogui.Button.Flags.RadioButton) button.setTooltip("Short tooltip!") button = nanogui.Button(layer, "Radio2 (Hover for Tooltip)") button.setFont(radio_font) button.setFlags(nanogui.Button.Flags.RadioButton) button.setTooltip( "This is a much longer tooltip that will get wrapped automatically!") button = nanogui.Button(layer, "ToggleButton") button.setFlags(nanogui.Button.Flags.ToggleButton) # checkbox (top level) nanogui.CheckBox(layer, "A CheckBox") # test the graph widget fonts layer = tabWidget.createTab("Graph") layer.setLayout(nanogui.GroupLayout()) # Same as nanogui python/example1.py nanogui.Label(layer, "Function Graph Widget") graph = nanogui.Graph(layer, "Some Function") graph.setHeader("E = 2.35e-3") graph.setFooter("Iteration 89") values = [ 0.5 * (0.5 * math.sin(i / 10.0) + 0.5 * math.cos(i / 23.0) + 1) for i in range(100) ] graph.setValues(values) # Dummy tab used to represent the last tab button. tabWidget.createTab("+") def tab_cb(index): if index == (tabWidget.tabCount() - 1): if themeChoice != ThemeChoice.Default: counter = screen.manual_counter else: counter = screen.default_counter # When the "+" tab has been clicked, simply add a new tab. tabName = "Dynamic {0}".format(counter) layerDyn = tabWidget.createTab(index, tabName) layerDyn.setLayout(nanogui.GroupLayout()) nanogui.Label(layerDyn, "Function graph widget", "spectral-bold") graphDyn = nanogui.Graph(layerDyn, "Dynamic function") graphDyn.setHeader("E = 2.35e-3") graphDyn.setFooter("Iteration {0}".format(index * counter)) valuesDyn = [ 0.5 * abs((0.5 * math.sin(i / 10.0 + counter)) + (0.5 * math.cos(i / 23.0 + 1 + counter))) for i in range(100) ] graphDyn.setValues(valuesDyn) if themeChoice != ThemeChoice.Default: screen.manual_counter += 1 else: screen.default_counter += 1 # We must invoke perform layout from the screen instance to keep everything in order. # This is essential when creating tabs dynamically. screen.performLayout() # Ensure that the newly added header is visible on screen tabWidget.ensureTabVisible(index) tabWidget.setCallback(tab_cb) tabWidget.setActiveTab(0) return window
def reset_cb(): cp.reset(nanogui.Color(r, g, b, a))
def alpha_cb(val): r = float(self.redBox.value()) / 255.0 g = float(self.greenBox.value()) / 255.0 b = float(self.blueBox.value()) / 255.0 a = float(val) / 255.0 self.reset(nanogui.Color(r, g, b, a), True)
def __init__(self, ctx): super(CustomTheme, self).__init__(ctx) # override the default fonts self.mDefaultFont = "spectral" self.mDefaultBoldFont = "spectral-bold" # two additional fonts loaded that are not defaults, but can be used # independently e.g. new Label(parent, "label", "spirax") self.mSpiraxFont = nanogui.createFontMem(ctx, "spirax", "Spirax-Regular.ttf") self.mMembraFont = nanogui.createFontMem(ctx, "membra", "membra.ttf") if self.mSpiraxFont == -1 or self.mMembraFont == -1: raise RuntimeError( "Could not load the `spirax` or `membra` fonts!") # load the default fonts for this theme self.mSpectralFont = nanogui.createFontMem(ctx, self.mDefaultFont, "SpectralSC-Regular.ttf") self.mSpectralBoldFont = nanogui.createFontMem(ctx, self.mDefaultBoldFont, "SpectralSC-Bold.ttf") if self.mSpectralFont == -1 or self.mSpectralBoldFont == -1: raise RuntimeError("Could not load the `spectral` fonts!") # overriding some default icons as demonstration # default: ``entypo.ICON_CHECK`` self.mCheckBoxIcon = entypo.ICON_CROSS self.mCheckBoxIconExtraScale = 1.3 # default: ``entypo.ICON_CHEVRON_RIGHT`` self.mPopupChevronRightIcon = entypo.ICON_TRIANGLE_RIGHT # default: ``entypo.ICON_CHEVRON_LEFT`` self.mPopupChevronLeftIcon = entypo.ICON_TRIANGLE_LEFT self.mPopupIconExtraScale = 0.8 # default: ``entypo.ICON_ARROW_BOLD_LEFT`` self.mTabHeaderLeftIcon = entypo.ICON_ARROW_WITH_CIRCLE_LEFT # default: ``entypo.ICON_ARROW_BOLD_RIGHT`` self.mTabHeaderRightIcon = entypo.ICON_ARROW_WITH_CIRCLE_RIGHT # default: ``entypo.ICON_CHEVRON_UP`` self.mTextBoxUpIcon = entypo.ICON_TRIANGLE_UP # default: ``entypo.ICON_CHEVRON_DOWN`` self.mTextBoxDownIcon = entypo.ICON_TRIANGLE_DOWN self.mTextBoxIconExtraScale = 0.6 self.mStandardFontSize = 16.0 self.mButtonFontSize = 20.0 self.mTextBoxFontSize = 20.0 self.mWindowFontSize = 18.0 self.mIconScale = 0.7699999809265137 self.mWindowCornerRadius = 2 self.mWindowHeaderHeight = 30 self.mWindowDropShadowSize = 10 self.mButtonCornerRadius = 2 self.mTabBorderWidth = 0.75 self.mTabInnerMargin = 5 self.mTabMinButtonWidth = 20 self.mTabMaxButtonWidth = 160 self.mTabControlWidth = 20 self.mTabButtonHorizontalPadding = 10 self.mTabButtonVerticalPadding = 2 self.mDropShadow = nanogui.Color(0.0, 0.16862745583057404, 0.21176475286483765, 1.0) self.mTransparent = nanogui.Color(0.0, 0.0, 0.0, 0.0) self.mBorderDark = nanogui.Color(0.4627450406551361, 0.5333333015441895, 0.5568627119064331, 0.9019607901573181) self.mBorderLight = nanogui.Color(0.6431373953819275, 0.6901960968971252, 0.7058823108673096, 0.9019607901573181) self.mBorderMedium = nanogui.Color(0.564706027507782, 0.6196078658103943, 0.6392157077789307, 0.9019607901573181) self.mTextColor = nanogui.Color(0.34117648005485535, 0.42745086550712585, 0.4509802758693695, 1.0) self.mDisabledTextColor = nanogui.Color(0.6078431010246277, 0.658823549747467, 0.6745098233222961, 0.9019607901573181) self.mTextColorShadow = nanogui.Color(0.9333333373069763, 0.9098039269447327, 0.8352941870689392, 0.9019607901573181) self.mIconColor = nanogui.Color(0.34117648005485535, 0.42745086550712585, 0.4509802758693695, 1.0) self.mButtonGradientTopFocused = nanogui.Color(0.7576307058334351, 0.7828220129013062, 0.789692223072052, 1.0) self.mButtonGradientBotFocused = nanogui.Color(0.6495736837387085, 0.671172022819519, 0.6770622730255127, 1.0) self.mButtonGradientTopUnfocused = nanogui.Color( 0.7975060343742371, 0.8240231871604919, 0.8312549591064453, 1.0) self.mButtonGradientBotUnfocused = nanogui.Color( 0.683761715888977, 0.7064968347549438, 0.7126971483230591, 1.0) self.mButtonGradientTopPushed = nanogui.Color(0.6495736241340637, 0.671172022819519, 0.6770622730255127, 1.0) self.mButtonGradientBotPushed = nanogui.Color(0.5569282174110413, 0.5754461288452148, 0.5804961919784546, 1.0) self.mTooltipBackgroundColor = nanogui.Color(0.027450978755950928, 0.21176469326019287, 0.2588234543800354, 0.9019607901573181) self.mTooltipTextColor = nanogui.Color(0.7628562450408936, 0.7953678369522095, 0.7991926670074463, 1.0) # Window-related self.mWindowFillUnfocused = nanogui.Color(0.8862745761871338, 0.8627451062202454, 0.7921568155288696, 0.9019607901573181) self.mWindowFillFocused = nanogui.Color(0.9333333373069763, 0.9098039269447327, 0.8352941870689392, 0.9019607901573181) self.mWindowTitleUnfocused = nanogui.Color(0.654053807258606, 0.6819284558296204, 0.6852078437805176, 1.0) self.mWindowTitleFocused = nanogui.Color(0.7628562450408936, 0.7953678369522095, 0.7991926670074463, 1.0) self.mWindowHeaderGradientTop = nanogui.Color(0.16616077721118927, 0.3241867423057556, 0.3645337224006653, 0.9019607901573181) self.mWindowHeaderGradientBot = nanogui.Color(0.027450978755950928, 0.21176469326019287, 0.2588234543800354, 0.9019607901573181) self.mWindowHeaderSepTop = nanogui.Color(0.28508710861206055, 0.4205746054649353, 0.45516711473464966, 0.9019607901573181) self.mWindowHeaderSepBot = nanogui.Color(0.0, 0.16862745583057404, 0.21176475286483765, 1.0) self.mWindowPopup = nanogui.Color(0.9333333373069763, 0.9098039269447327, 0.8352941870689392, 0.9019607901573181) self.mWindowPopupTransparent = nanogui.Color(0.19607843458652496, 0.19607843458652496, 0.19607843458652496, 0.0)