Skip to content

Commit

Permalink
16
Browse files Browse the repository at this point in the history
  • Loading branch information
villares committed Oct 17, 2023
1 parent 5259174 commit 711e780
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
82 changes: 82 additions & 0 deletions 2023/sketch_2023_10_16/buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import py5
s = py5.get_current_sketch()

B_RADIUS = 0


class Button():

button_list = []

def __init__(self, x, y, w, h, txt, func):
self.x, self.y = x, y
self.w, self.h = w, h
self.txt = txt
self.txt_color = py5.color(0)
self.pressed = False
self.func = func
self.active = False
self.active_on = py5.color(200, 200, 240)
self.active_off = py5.color(100)
self.button_list.append(self)

def mouse_over(self):
return (self.x < s.mouse_x < self.x + self.w and
self.y < s.mouse_y < self.y + self.h)

def display(self, mp):
mouse_over = self.mouse_over()
py5.push()
py5.stroke_weight(1)
py5.stroke(0)
py5.fill(self.calc_fill(mouse_over))
py5.rect_mode(py5.CORNER)
py5.rect(self.x, self.y, self.w, self.h, B_RADIUS)
py5.fill(self.txt_color)
py5.text_align(py5.CENTER, py5.CENTER)
py5.text(self.txt,
self.x + self.w / 2,
self.y + self.h / 2)
if self.check(mouse_over, mp):
self.func(self)
py5.pop()

def check(self, mouse_over, mp):
result = False
if mouse_over and self.pressed and not mp:
result = True
if mouse_over and mp:
self.pressed = True
else:
self.pressed = False
return result

def toggle(self):
self.active = not self.active

def exclusive_on(self):
for b in self.button_list:
b.active = False
self.active = True

def calc_fill(self, mouse_over):
if self.active and mouse_over:
return self.darken_color(self.active_on)
elif self.active:
return self.active_on
elif mouse_over:
return self.darken_color(self.active_off)
else:
return self.active_off

@staticmethod
def darken_color(c):
r, g, b = py5.red(c), py5.green(c), py5.blue(c)
return py5.color(r / 2, g / 2, b / 2)

@classmethod
def display_all(cls, mp):
for b in cls.button_list:
b.display(mp)


Binary file added 2023/sketch_2023_10_16/sketch_2023_10_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions 2023/sketch_2023_10_16/sketch_2023_10_16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

line_height = 60

def setup():
global terms
size(600, 600)
text_size(line_height)
color_mode(HSB)
terms = setup_terms()

def draw():
background(0, 0, 200)
draw_terms()

def draw_terms():
for term in terms:
x, y = terms[term]['x'], terms[term]['y']
w, h = terms[term]['w'], terms[term]['h']
selected = terms[term]['state']
if mouse_over_term(term):
no_fill()
rect(x - 2, y, w + 4, h)
if selected:
fill(255)
else:
fill(0)
text(term, x, y + h * 0.75)

def mouse_released():
if is_key_pressed:
non_exclusive_selection()
else:
exclusive_selection()

def non_exclusive_selection():
for term in terms:
if mouse_over_term(term):
terms[term]['state'] ^= 1

def exclusive_selection():
for term in terms:
if mouse_over_term(term):
terms[term]['state'] = True
else:
terms[term]['state'] = False


def mouse_over_term(term):
x, y = terms[term]['x'], terms[term]['y']
w, h = terms[term]['w'], terms[term]['h']
return (x < mouse_x < x + w
and y < mouse_y < y + h)

def pos(i, t, lw, lh=None, wgap=20, hgap=2):
# set pos.x, pos.xo, pox.y before you call this
lh = lh or text_ascent() + text_descent()
pos.tw = text_width(t)
if pos.x + pos.tw > lw:
pos.x = pos.xo
pos.y += lh + hgap
x = pos.x
pos.x += pos.tw + wgap
return x

def setup_terms():
ls = load_strings("terms.txt")
term_names = [term for term in ls
if term and not '(' in term
and not term.startswith('\t')]
pos.x = pos.xo = pos.y = 20 # initial x and y
terms = {term: {'state': False,
'x': pos(i, term, width),
'y': pos.y,
'w': pos.tw,
'h': line_height,
}
for i, term in enumerate(term_names)
}
println(len(terms))
return terms
42 changes: 42 additions & 0 deletions 2023/sketch_2023_10_16/sketch_2023_10_16_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from buttons import Button

estado_inicial = True

BLUE, RED = color(0, 0, 200), color(255, 0, 0)
b_color = BLUE

def setup():
global b0, b1, show_hide
size(400, 400)
show_hide = Button(100, 100, 100, 50,
txt="show\noptions",
func=Button.toggle)
b0 = Button(100, 150, 100, 50,
txt="blue\nbackground",
func=bgnd_setter(BLUE))
b0.active = True
b1 = Button(100, 200, 100, 50,
txt="red\nbackgound",
func=bgnd_setter(RED))

def draw():
background(b_color)
show_hide.display(is_mouse_pressed)
if show_hide.active:
b0.display(is_mouse_pressed)
b1.display(is_mouse_pressed)

def bgnd_setter(c):
def setter(button):
global b_color
button.exclusive_on()
b_color = c
return setter

# def red_BLUE(button):
# global b_color
# button.toggle()
# if button..active:
# b_color = color(255, 0, 0)
# else:
# b_color = color(0)
8 changes: 8 additions & 0 deletions 2023/sketch_2023_10_16/terms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
itertools
functools
random
pathlib
tomlib
collections
trimesh
shapely
10 changes: 10 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ Here are listed some of the tools I have been using:

2023 \| [<b>2022</b>](2022.md) \| [<b>2021</b>](2021.md) \| [<b>2020</b>](2020.md) \| [<b>2019</b>](2019.md) \| [<b>2018</b>](2018.md)

---

### sketch_2023_10_16

![sketch_2023_10_16](https://raw.githubusercontent.com/villares/sketch-a-day/main/2023/sketch_2023_10_16/sketch_2023_10_16.png)

[sketch_2023_10_16](https://github.com/villares/sketch-a-day/tree/main/2023/sketch_2023_10_16) [[py5](https://py5coding.org/)]



---

### sketch_2023_10_15
Expand Down

0 comments on commit 711e780

Please sign in to comment.