Skip to content

Commit

Permalink
[stable] dynamic rendered dialogue back
Browse files Browse the repository at this point in the history
  • Loading branch information
sakharovaan committed Nov 8, 2021
1 parent 6496534 commit 1566226
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 13 deletions.
35 changes: 35 additions & 0 deletions dialogue_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import yaml
import tkinter as tk
import random


class DialoguePlugin:
def __init__(self, window, _ghostconfig):
self.w = window
self.blinked = False

with open(_ghostconfig) as f:
self._config = yaml.safe_load(f.read())

self.grip = None

def _render_back(self):
h_cursor = 0
h_top = self.w.image.getdlimg('top').height()
h_middle = self.w.image.getdlimg('middle').height()
h_bottom = self.w.image.getdlimg('bottom').height()
desiredwidth = 600

self.w.grip.create_image(self._config['dialogue']['offset'], h_cursor, image=self.w.image.getdlimg('top'), anchor='nw', tags=('dialogue',))
h_cursor += h_top

while h_cursor + h_bottom < desiredwidth:
self.w.grip.create_image(self._config['dialogue']['offset'], h_cursor, image=self.w.image.getdlimg('middle'), anchor='nw', tags=('dialogue',))
h_cursor += h_middle

self.w.grip.create_image(self._config['dialogue']['offset'], h_cursor, image=self.w.image.getdlimg('bottom'), anchor='nw', tags=('dialogue',))
self.w.grip.pack(side="right", fill="both", expand=True)

def _hide_back(self):
for c in self.w.grip.find_withtag('dialogue'):
self.w.grip.delete(c)
6 changes: 3 additions & 3 deletions expression_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def __init__(self, window, _ghostconfig):
window.app.after(10, self.forced_tick())

def _random_expression_init(self):
self.w.grip.create_image(0, 0, image=self.w.image.getimg('direct_v_cat', 'closed'), anchor='nw', tags=("image_closed",))
self.w.grip.create_image(0, 0, image=self.w.image.getimg('direct_v_cat', 'opened'), anchor='nw', tags=("image_open",))
self.w.grip.pack()
self.w.grip.create_image(self._config['ghost']['width'], 0, image=self.w.image.getimg('direct_v_cat', 'closed'), anchor='nw', tags=("image_closed",))
self.w.grip.create_image(self._config['ghost']['width'], 0, image=self.w.image.getimg('direct_v_cat', 'opened'), anchor='nw', tags=("image_open",))
self.w.grip.pack(side="right", fill="both", expand=True)

def random_tick(self):
newexpr = random.choice(self.w.image.getexprlist())
Expand Down
14 changes: 12 additions & 2 deletions ghost.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ timings:
min: 30000
max: 120000

size:
ghost:
width: 450
height: 450

dialogue:
width: 450
offset: 150
elements:
- name: bottom
file: 'static/dialogue/bottom.png'
- name: middle
file: 'static/dialogue/middle.png'
- name: top
file: 'static/dialogue/top.png'
19 changes: 15 additions & 4 deletions imageloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self, ghostconfig):
self._ghostconfig = ghostconfig
self._imagecache = {} #cache of raw/rescaled files
self._imagebase = {} #cache of rendered layer files
self._dialoguebase = {}

with open(self._ghostconfig) as f:
self._config = yaml.safe_load(f.read())
Expand All @@ -21,6 +22,9 @@ def __init__(self, ghostconfig):
for m in self._config['elements']['mouth']:
self._imagebase[f"{e['name']}_{b['name']}_{m['name']}"] = self._render_ebm(e, b, m)

for d in self._config['dialogue']['elements']:
self._dialoguebase[d['name']] = self._render_dialogue(d)

def _render_ebm(self, e, b, m):
op = self._loadimage(self._config['background']['file'])

Expand All @@ -36,13 +40,17 @@ def _render_ebm(self, e, b, m):
'closed': ImageTk.PhotoImage(cl),
'mood': list(m.get('mood', []) + b.get('mood', []) + e.get('mood', []))}

def _loadimage(self, path):
def _render_dialogue(self, d):
return ImageTk.PhotoImage(self._loadimage(d['file'], confsource="dialogue"))

def _loadimage(self, path, confsource="ghost"):
if path in self._imagecache:
return self._imagecache[path]
else:
self._imagecache[path] = self.RBGAImage(path).resize((
self._config['size']['width'],
self._config['size']['height']))
with self.RBGAImage(path) as im:
wpercent = (self._config[confsource]['width'] / float(im.size[0]))
hsize = int((float(im.size[1]) * float(wpercent)))
self._imagecache[path] = im.resize((self._config[confsource]['width'], hsize), Image.BICUBIC)

return self._imagecache[path]

Expand All @@ -58,3 +66,6 @@ def getmood(self, ident):

def getexprlist(self):
return list(self._imagebase.keys())

def getdlimg(self, pos):
return self._dialoguebase[pos]
9 changes: 5 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from imageloader import ImageLoader
from blink_plugin import BlinkPlugin
from expression_plugin import ExpressionPlugin
from dialogue_plugin import DialoguePlugin


class App(tk.Tk):
Expand All @@ -24,15 +25,16 @@ def __init__(self, *args, **kwargs):
self.wm_attributes("-transparentcolor", "brown")
self.image = ImageLoader('ghost.yaml')

self.grip = tk.Canvas(self, width=450, height=450, background="brown", bd=0, highlightthickness=0, relief='ridge')
self.grip = tk.Canvas(self, width=450+450, height=1000, background="brown", bd=0, highlightthickness=0, relief='ridge')

self.ep = ExpressionPlugin(self, 'ghost.yaml')
self.bp = BlinkPlugin(self, 'ghost.yaml')
self.dp = DialoguePlugin(self, 'ghost.yaml')

self.menu = tk.Menu(self, tearoff=0)
self.menu.add_command(label="Next expression", command=self.ep.random_tick)
self.menu.add_command(label="Copy", command=lambda: self.menu_callback("2"))
self.menu.add_command(label="Paste", command=lambda: self.menu_callback("3"))
self.menu.add_command(label="Show dialogue", command=self.dp._render_back)
self.menu.add_command(label="Hide dialogue", command=self.dp._hide_back)
self.menu.add_command(label="Reload", command=lambda: self.menu_callback("4"))
self.menu.add_checkbutton(label="add_checkbutton")
self.menu.add_separator()
Expand All @@ -43,7 +45,6 @@ def __init__(self, *args, **kwargs):
self.grip.bind("<ButtonRelease-1>", self.left_release)
self.grip.bind("<B1-Motion>", self.do_move)


@staticmethod
def RBGAImage(path):
return Image.open(path).convert("RGBA")
Expand Down
Binary file added static/dialogue/bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/dialogue/middle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/dialogue/top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1566226

Please sign in to comment.