Skip to content

Commit

Permalink
Cleaned up button code, fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwijuice56 committed Jan 25, 2024
1 parent 6f78b17 commit 4527e52
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 134 deletions.
93 changes: 93 additions & 0 deletions gui/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import pyglet


class Button(pyglet.gui.WidgetBase):
BACKGROUND_COLOR = (0, 0, 0, 140)
HOVER_COLOR = (64, 64, 64, 140)

def __init__(self, bounding_box, text, index, window, batch, bg_batch):
pyglet.gui.WidgetBase.__init__(self, *bounding_box)
self.batch = batch
self.window = window
window.push_handlers(self.on_mouse_press, self.on_mouse_motion)

self.bounding_box = bounding_box

self.text = text
self.index = index

self.background = pyglet.shapes.Rectangle(*self.bounding_box, color=self.BACKGROUND_COLOR[0:3], batch=bg_batch)
self.background.opacity = self.BACKGROUND_COLOR[3]
self.label = pyglet.text.Label(
text=self.text, font_name="Consolas", multiline=True,
font_size=16, x=self.x, y=self.y + 28, width=self.width,
anchor_x="left", anchor_y="top", batch=self.batch)
self.visible = False
self.pressed = False
self.hovered = False

def show(self):
self.background.opacity = self.BACKGROUND_COLOR[3]
self.label.batch = self.batch
self.visible = True

def hide(self):
self.background.color = self.BACKGROUND_COLOR[0: 3]
self.background.opacity = 0
self.label.batch = None
self.visible = False
self.hovered = False

def on_mouse_press(self, x, y, button, modifiers):
if not self.visible:
return
b = [c for c in self.bounding_box]
y -= self.window.height
if x > b[0] and x - b[0] < b[2] and y > b[1] and y - b[1] < b[3]:
self.pressed = True
return pyglet.event.EVENT_HANDLED

def on_mouse_motion(self, x, y, dx, dy):
if not self.visible:
return
b = [c for c in self.bounding_box]
y -= self.window.height
if x > b[0] and x - b[0] < b[2] and y > b[1] and y - b[1] < b[3]:
self.background.color = self.HOVER_COLOR[0: 3]
self.hovered = True
elif self.hovered:
self.background.color = self.BACKGROUND_COLOR[0: 3]
self.hovered = False


class DropDown(Button):
def __init__(self, bounding_box, title, options, window, batch, bg_batch):
Button.__init__(self, bounding_box, '%-21s ▾' % options[0], -1, window, batch, bg_batch)
self.batch = batch
self.window = window
self.window.push_handlers(self.on_mouse_press, self.on_mouse_motion)

self.options = options
self.buttons = []
for i, option in enumerate(self.options):
button = Button([self.x, self.y - 32 * (i + 1), self.width, 32], self.options[i], i, self.window, self.batch, bg_batch)
self.buttons.append(button)

self.title = pyglet.text.Label(
text=title, font_name="Consolas", multiline=True,
font_size=16, x=self.x, y=self.bounding_box[1] + 64, width=self.width,
anchor_x="left", anchor_y="top", batch=self.batch)

self.is_open = False
self.close()
self.show()

def open(self):
self.is_open = True
for button in self.buttons:
button.show()

def close(self):
self.is_open = False
for button in self.buttons:
button.hide()
147 changes: 13 additions & 134 deletions gui/user_interface.py
Original file line number Diff line number Diff line change
@@ -1,137 +1,8 @@
import math

import pyglet
from pyglet.gl import *


class DropDown(pyglet.gui.WidgetBase):
BACKGROUND_COLOR = (0, 0, 0, 140)

def __init__(self, x, y, width, height, title, options, window, batch, bg_batch):
pyglet.gui.WidgetBase.__init__(self, x, y, width, height)
self.batch = batch
self.window = window
self.window.push_handlers(self.on_mouse_press, self.on_mouse_drag)

self.bounding_box = [x, y, width, height]

self.title = title
self.options = options
self.selected_option = options[0]
self.is_open = False

self.batch = batch
self.buttons = []
for i, option in enumerate(self.options):
button = Button(self.x, self.y - 32 * (i + 2), self.width, 32, self.options[i], i, self.window, self.batch, bg_batch)
self.buttons.append(button)

self.title = pyglet.text.Label(
text=self.title, font_name="Consolas", multiline=True,
font_size=16, x=self.x, y=-20, width=self.width,
anchor_x="left", anchor_y="top", batch=self.batch)

self.current_option = pyglet.text.Label(
text='%-21s ▾' % self.selected_option, font_name="Consolas", multiline=True,
font_size=16, x=self.x, y=-52, width=self.width,
anchor_x="left", anchor_y="top", batch=self.batch)

self.background = pyglet.shapes.Rectangle(*self.bounding_box, color=self.BACKGROUND_COLOR[0:3],
batch=bg_batch)
self.background.y -= 32
self.background.opacity = self.BACKGROUND_COLOR[3]

self.close()

def on_mouse_press(self, x, y, button, modifiers):
y = y - self.window.height
box = [c for c in self.bounding_box]
box[1] -= 32
if not (x > box[0] and x - box[0] < box[2] and
y > box[1] and y - box[1] < box[3]) or self.is_open:
self.close()
else:
self.open()
return pyglet.event.EVENT_HANDLED

def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
y = y - self.window.height
box = [c for c in self.bounding_box]
box[1] -= 32
if x > box[0] and x - box[0] < box[2] and y > box[1] and y - box[1] < box[3]:
return pyglet.event.EVENT_HANDLED

def open(self):
self.is_open = True
for button in self.buttons:
button.show()

def close(self):
self.is_open = False
for button in self.buttons:
button.hide()


class Button(pyglet.gui.WidgetBase):
BACKGROUND_COLOR = (0, 0, 0, 140)
HOVER_COLOR = (64, 64, 64, 190)

def __init__(self, x, y, width, height, text, index, window, batch, bg_batch):
pyglet.gui.WidgetBase.__init__(self, x, y, width, height)
self.batch = batch
self.window = window
window.push_handlers(self.on_mouse_press, self.on_mouse_motion)

self.bounding_box = [x, y, width, height]
self.text = text
self.index = index

self.background = pyglet.shapes.Rectangle(*self.bounding_box, color=self.BACKGROUND_COLOR[0:3],
batch=bg_batch)
self.background.opacity = self.BACKGROUND_COLOR[3]
self.label = pyglet.text.Label(
text=self.text, font_name="Consolas", multiline=True,
font_size=16, x=self.x, y=self.y + 28, width=self.width,
anchor_x="left", anchor_y="top", batch=self.batch)

self.visible = False
self.pressed = False
self.hovered = False

def show(self):
self.label.batch = self.batch
self.background.batch = self.batch
self.background.color = self.BACKGROUND_COLOR[0: 3]
self.background.opacity = self.BACKGROUND_COLOR[3]
self.visible = True

def hide(self):
self.label.batch = None
self.background.opacity = 0
self.visible = False

def on_mouse_press(self, x, y, button, modifiers):
if not self.visible:
return

y = y - self.window.height
box = [c for c in self.bounding_box]
if x > box[0] and x - box[0] < box[2] and y > box[1] and y - box[1] < box[3]:
self.pressed = True
return pyglet.event.EVENT_HANDLED

def on_mouse_motion(self, x, y, dx, dy):
y = y - self.window.height
box = [c for c in self.bounding_box]
if self.visible and x > box[0] and x - box[0] < box[2] and y > box[1] and y - box[1] < box[3]:
self.background.color = self.HOVER_COLOR[0: 3]
self.background.opacity = self.HOVER_COLOR[3]
self.hovered = True
return pyglet.event.EVENT_HANDLED
elif self.hovered and self.visible:
self.hovered = False
self.background.color = self.BACKGROUND_COLOR[0: 3]
self.background.opacity = self.BACKGROUND_COLOR[3]
from gui.button import *


class UserInterface:
Expand All @@ -156,10 +27,10 @@ def __init__(self, protein, window, pdb_renderer, embedding_renderer):
self.res_doc = pyglet.text.document.FormattedDocument()
self.res_layout = None

self.color_mode = DropDown(x=16, y=-48, width=292, height=32, title="Coloring Mode",
self.color_mode = DropDown(bounding_box=[16, -80, 292, 32], title="Coloring Mode",
options=["Functional Similarity", "Amino Acid Order", "Atom Type"],
window=window, batch=self.batch, bg_batch=self.bg_batch)
self.color_palette = DropDown(x=324, y=-48, width=292, height=32, title="Color Palette",
self.color_palette = DropDown(bounding_box=[324, -80, 292, 32], title="Color Palette",
options=["Rainbow", "Monocolor", "Poisson", "Grape"],
window=window, batch=self.batch, bg_batch=self.bg_batch)

Expand Down Expand Up @@ -268,19 +139,27 @@ def draw(self):
glEnable(GL_BLEND)
glOrtho(0, self.window.width, -self.window.height, 0, 0, 1000)

for dropdown in [self.color_mode, self.color_palette]:
if dropdown.pressed:
dropdown.pressed = False
if dropdown.is_open:
dropdown.close()
else:
dropdown.open()

for button in self.color_mode.buttons:
if button.pressed:
button.pressed = False
self.color_mode.close()
self.color_mode.current_option.text = '%-21s ▾' % button.label.text
self.color_mode.label.text = '%-21s ▾' % button.label.text
self.protein.update_colors(new_color_mode=button.index + 1)
self.pdb_renderer.update_colors()
self.embedding_renderer.update_colors()
for button in self.color_palette.buttons:
if button.pressed:
button.pressed = False
self.color_palette.close()
self.color_palette.current_option.text = '%-21s ▾' % button.label.text
self.color_palette.label.text = '%-21s ▾' % button.label.text
self.protein.update_colors(new_color_palette=button.index + 6)
self.pdb_renderer.update_colors()
self.embedding_renderer.update_colors()
Expand Down

0 comments on commit 4527e52

Please sign in to comment.