forked from boskee/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
648 lines (580 loc) · 26.7 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
from math import floor, ceil
import os
from pyglet.sprite import Sprite
from pyglet.text import Label
from pyglet.gl import *
from pyglet.window import key
from blocks import *
from crafting import *
from globals import *
from inventory import *
from items import *
class Rectangle(object):
def __init__(self, x, y, width, height):
self.x, self.y = x, y
self.width, self.height = width, height
def set_position(self, x, y):
self.x, self.y = x, y
def set_size(self, width, height):
self.width, self.height = width, height
def get_center(self):
return self.x + self.width / 2, self.y + self.height / 2
def hit_test(self, x, y):
return (x >= self.x and x <= self.x + self.width) and (y >= self.y and y <= self.y + self.height)
@property
def min(self):
return (self.x, self.y)
@property
def max(self):
return (self.x + self.width, self.y + self.height)
class Button(Rectangle):
def __init__(self, x, y, width, height, image=None, caption=None, batch=None, group=None):
super(Button, self).__init__(x, y, width, height)
self.batch = batch
self.group = group
self.sprite = None
self.label = None
if image:
self.sprite = Sprite(image.get_region(0, 0, self.width, self.height), batch=self.batch, group=self.group)
if caption:
self.label = Label(caption, font_name='Arial', font_size=12,
anchor_x='center', anchor_y='center', color=(255, 255, 255, 255), batch=self.batch,
group=self.group)
self.set_position(x, y)
def set_position(self, x, y):
super(Button, self).set_position(x, y)
if self.sprite:
self.sprite.x, self.sprite.y = x, y
if self.label:
self.label.x, self.label.y = self.get_center()
def draw(self):
if self.sprite:
self.sprite.draw()
if self.label:
self.label.draw()
class ItemSelector(object):
def __init__(self, parent, player, model):
self.batch = pyglet.graphics.Batch()
self.group = pyglet.graphics.OrderedGroup(1)
self.labels_group = pyglet.graphics.OrderedGroup(2)
self.amount_labels = []
self.parent = parent
self.model = model
self.player = player
self.max_items = 9
self.current_index = 1
self.icon_size = self.model.group.texture.width / TILESET_SIZE
self.visible = True
self.num_keys = [
key._1, key._2, key._3, key._4, key._5,
key._6, key._7, key._8, key._9, key._0]
image = pyglet.image.load(os.path.join('resources', 'textures', 'slots.png'))
heart_image = pyglet.image.load(os.path.join('resources', 'textures', 'heart.png'))
frame_size = image.height / 2
self.frame = pyglet.sprite.Sprite(
image.get_region(0, frame_size, image.width, frame_size),
batch=self.batch, group=pyglet.graphics.OrderedGroup(0))
self.active = pyglet.sprite.Sprite(
image.get_region(0, 0, frame_size, frame_size), batch=self.batch,
group=pyglet.graphics.OrderedGroup(2))
self.hearts = []
for i in range(0, 10):
heart = pyglet.sprite.Sprite(
heart_image.get_region(0, 0, heart_image.width, heart_image.width),
batch=self.batch, group=pyglet.graphics.OrderedGroup(0))
self.hearts.append(heart)
self.current_block_label = None
def change_index(self, change):
self.set_index(self.current_index + change)
def set_index(self, index):
index = int(index)
if self.current_index == index:
return
self.current_index = index
if self.current_index >= self.max_items:
self.current_index = 0
elif self.current_index < 0:
self.current_index = self.max_items - 1
self.update_current()
def get_block_icon(self, block):
block_icon = None
if os.path.isfile(os.path.join('resources', 'textures', 'icons', str(block.id) + ".png")) == True:
block_icon = pyglet.image.load(os.path.join('resources', 'textures', 'icons', str(block.id) + ".png"))
else:
block_icon = self.model.group.texture.get_region(
int(block.side_texture[0] * TILESET_SIZE) * self.icon_size,
int(block.side_texture[1] * TILESET_SIZE) * self.icon_size, self.icon_size,
self.icon_size)
return block_icon
def update_items(self):
self.player.quick_slots.remove_unnecessary_stacks()
self.icons = []
for amount_label in self.amount_labels:
amount_label.delete()
self.amount_labels = []
x = self.frame.x + 3
items = self.player.quick_slots.get_items()
items = items[:self.max_items]
for item in items:
if not item:
x += (self.icon_size * 0.5) + 3
continue
block = BLOCKS_DIR[item.type]
block_icon = self.get_block_icon(block)
icon = pyglet.sprite.Sprite(block_icon, batch=self.batch,
group=self.group)
icon.scale = 0.5
icon.x = x
icon.y = self.frame.y + 3
item.quickslots_x = icon.x
item.quickslots_y = icon.y
x += (self.icon_size * 0.5) + 3
amount_label = pyglet.text.Label(
str(item.amount), font_name='Arial', font_size=9,
x=icon.x + 3, y=icon.y, anchor_x='left', anchor_y='bottom',
color=block.amount_label_color, batch=self.batch,
group=self.labels_group)
self.amount_labels.append(amount_label)
self.icons.append(icon)
self.update_current()
def update_current(self):
if self.current_block_label:
self.current_block_label.delete()
if hasattr(self.get_current_block_item(False), 'quickslots_x') and hasattr(self.get_current_block_item(False), 'quickslots_y'):
self.current_block_label = pyglet.text.Label(
self.get_current_block_item(False).name, font_name='Arial', font_size=9,
x=self.get_current_block_item(False).quickslots_x + 0.25 * self.icon_size, y=self.get_current_block_item(False).quickslots_y - 20,
anchor_x='center', anchor_y='bottom',
color=(255, 255, 255, 255), batch=self.batch,
group=self.labels_group)
self.active.x = self.frame.x + (self.current_index * 35)
def update_health(self):
hearts_to_show = self.player.health
showed_hearts = 0
for i, heart in enumerate(self.hearts):
heart.x = self.frame.x + i * (20 + 2) + (self.frame.width - hearts_to_show * (20 + 2)) / 2
heart.y = self.icon_size * 1.0 + 12
heart.opacity = 255
if showed_hearts >= hearts_to_show:
heart.opacity = 0
showed_hearts += 1
def get_current_block(self):
item = self.player.quick_slots.at(self.current_index)
if not item:
return
item_id = item.type
if item_id >= ITEM_ID_MIN:
return ITEMS_DIR[item_id]
return BLOCKS_DIR[item_id]
def get_current_block_item(self, remove=False):
item = self.player.quick_slots.at(self.current_index)
if remove:
self.player.quick_slots.remove_by_index(self.current_index,
quantity=item.amount)
return item
def get_current_block_item_and_amount(self, remove=True):
item = self.player.quick_slots.at(self.current_index)
if item:
amount = item.amount
if remove:
self.player.quick_slots.remove_by_index(self.current_index,
quantity=item.amount)
return item, amount
return False
def remove_current_block(self, quantity=1):
self.player.quick_slots.remove_by_index(self.current_index, quantity=quantity)
self.update_items()
def toggle(self):
self.visible = not self.visible
if self.visible:
self.update_items()
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
if self.visible and self.parent.window.exclusive:
self.change_index(scroll_y * -1)
return pyglet.event.EVENT_HANDLED
def on_key_press(self, symbol, modifiers):
if self.visible:
if symbol in self.num_keys:
index = (symbol - self.num_keys[0])
self.set_index(index)
return pyglet.event.EVENT_HANDLED
elif symbol == key.ENTER:
current_block = self.get_current_block_item_and_amount()
if current_block:
if not self.player.inventory.add_item(
current_block[0].id, quantity=current_block[1]):
self.player.quick_slots.add_item(
current_block[0].id, quantity=current_block[1])
self.update_items()
return pyglet.event.EVENT_HANDLED
def on_resize(self, width, height):
self.frame.x = (width - self.frame.width) / 2
self.frame.y = self.icon_size * 0.5
self.active.y = self.frame.y
if self.visible:
self.update_health()
self.update_current()
self.update_items()
def draw(self):
if self.visible:
self.batch.draw()
class InventorySelector(object):
def __init__(self, parent, player, model):
self.batch = pyglet.graphics.Batch()
self.group = pyglet.graphics.OrderedGroup(1)
self.amount_labels_group = pyglet.graphics.OrderedGroup(2)
self.amount_labels = []
self.parent = parent
self.model = model
self.player = player
self.max_items = self.player.inventory.slot_count
self.current_index = 1
self.icon_size = self.model.group.texture.width / TILESET_SIZE
self.selected_item = None
self.selected_item_icon = None
image = pyglet.image.load(os.path.join('resources', 'textures', 'inventory.png'))
self.frame = pyglet.sprite.Sprite(image.get_region(0, 0, image.width, image.height), batch=self.batch, group=pyglet.graphics.OrderedGroup(0))
self.crafting_panel = Inventory(4)
self.crafting_outcome = None # should be an item stack
self.crafting_outcome_icon = None
#self.active = pyglet.sprite.Sprite(image.get_region(0, 0, image.height / 4, image.height / 4), batch=self.batch, group=pyglet.graphics.OrderedGroup(2))
#self.active.opacity = 0
self.frame.x = (parent.window.width - self.frame.width) / 2
self.frame.y = self.icon_size / 2 - 4
self.visible = False
def change_index(self, change):
self.set_index(self.current_index + change)
def set_index(self, index):
index = int(index)
if self.current_index == index:
return
self.current_index = index
if self.current_index >= self.max_items:
self.current_index = 0
elif self.current_index < 0:
self.current_index = self.max_items - 1
self.update_current()
def get_block_icon(self, block):
block_icon = None
if os.path.isfile(os.path.join('resources', 'textures', 'icons', str(block.id) + ".png")) == True:
block_icon = pyglet.image.load(os.path.join('resources', 'textures', 'icons', str(block.id) + ".png"))
else:
block_icon = self.model.group.texture.get_region(
int(block.side_texture[0] * TILESET_SIZE) * self.icon_size,
int(block.side_texture[1] * TILESET_SIZE) * self.icon_size, self.icon_size,
self.icon_size)
return block_icon
def update_items(self):
rows = floor(self.max_items / 9)
inventory_y = 43
inventory_height = (rows * (self.icon_size * 0.5)) + ((rows+1) * 3)
self.icons = []
for amount_label in self.amount_labels:
amount_label.delete()
self.amount_labels = []
x = self.frame.x + 7
y = self.frame.y + inventory_y + inventory_height
items = self.player.inventory.get_items()
items = items[:self.max_items]
for i, item in enumerate(items):
if not item:
x += (self.icon_size * 0.5) + 3
if x >= (self.frame.x + self.frame.width) - 7:
x = self.frame.x + 7
y -= (self.icon_size * 0.5) + 3
continue
block = BLOCKS_DIR[item.type]
block_icon = self.get_block_icon(block)
icon = pyglet.sprite.Sprite(block_icon, batch=self.batch,
group=self.group)
icon.scale = 0.5
icon.x = x
icon.y = y - icon.height
x += (self.icon_size * 0.5) + 3
if x >= (self.frame.x + self.frame.width) - 7:
x = self.frame.x + 7
y -= (self.icon_size * 0.5) + 3
amount_label = pyglet.text.Label(
str(item.amount), font_name='Arial', font_size=9,
x=icon.x + 3, y=icon.y, anchor_x='left', anchor_y='bottom',
color=block.amount_label_color, batch=self.batch,
group=self.amount_labels_group)
self.amount_labels.append(amount_label)
self.icons.append(icon)
items = self.player.quick_slots.get_items()
items = items[:self.player.quick_slots.slot_count]
for i, item in enumerate(items):
if not item:
x += (self.icon_size * 0.5) + 3
continue
block = BLOCKS_DIR[item.type]
block_icon = self.get_block_icon(block)
icon = pyglet.sprite.Sprite(block_icon, batch=self.batch,
group=self.group)
icon.scale = 0.5
icon.x = x
icon.y = self.frame.y + 7
item.quickslots_x = icon.x
item.quickslots_y = icon.y
x += (self.icon_size * 0.5) + 3
amount_label = pyglet.text.Label(
str(item.amount), font_name='Arial', font_size=9,
x=icon.x + 3, y=icon.y, anchor_x='left', anchor_y='bottom',
color=block.amount_label_color, batch=self.batch,
group=self.amount_labels_group)
self.amount_labels.append(amount_label)
self.icons.append(icon)
self.update_current()
crafting_y = inventory_y + inventory_height + 42
crafting_rows = 2
crafting_height = (crafting_rows * (self.icon_size * 0.5)) + (crafting_rows * 3)
x = self.frame.x + 165
y = self.frame.y + crafting_y + crafting_height
items = self.crafting_panel.get_items()
items = items[:self.crafting_panel.slot_count]
# NOTE: each line in the crafting panel should be a sub-list in the crafting ingredient list
crafting_ingredients = [[], []]
for i, item in enumerate(items):
if not item:
x += (self.icon_size * 0.5) + 3
if x >= (self.frame.x + 165) + 67:
x = self.frame.x + 165
y -= (self.icon_size * 0.5) + 3
continue
block = BLOCKS_DIR[item.type]
block_icon = self.get_block_icon(block)
icon = pyglet.sprite.Sprite(block_icon, batch=self.batch,
group=self.group)
icon.scale = 0.5
icon.x = x
icon.y = y - icon.height
item.quickslots_x = icon.x
item.quickslots_y = icon.y
x += (self.icon_size * 0.5) + 3
if x >= (self.frame.x + 165) + 67:
x = self.frame.x + 165
y -= (self.icon_size * 0.5) + 3
amount_label = pyglet.text.Label(
str(item.amount), font_name='Arial', font_size=9,
x=icon.x + 3, y=icon.y, anchor_x='left', anchor_y='bottom',
color=block.amount_label_color, batch=self.batch,
group=self.amount_labels_group)
self.amount_labels.append(amount_label)
self.icons.append(icon)
if block.id > 0:
crafting_ingredients[int(floor(i / 2))].append(block)
if len(crafting_ingredients) > 0:
outcome = recipes.craft(crafting_ingredients)
if outcome:
self.set_crafting_outcome(outcome)
elif self.crafting_outcome:
self.remove_crafting_outcome()
self.update_current()
def update_current(self):
'''self.active.x = self.frame.x + ((self.current_index % 9) * self.icon_size * 0.5) + (self.current_index % 9) * 3
self.active.y = self.frame.y + floor(self.current_index / 9) * self.icon_size * 0.5 + floor(self.current_index / 9) * 6'''
def get_current_block_item_and_amount(self):
item = self.player.inventory.at(self.current_index)
if item:
amount = item.amount
self.player.inventory.remove_by_index(self.current_index, quantity=item.amount)
return item, amount
return False
def toggle(self):
if not self.visible:
self.update_items()
self.parent.item_list.toggle()
self.parent.window.set_exclusive_mouse(self.visible)
self.visible = not self.visible
def mouse_coords_to_index(self, x, y):
inventory_rows = floor(self.max_items / 9)
crafting_rows = 2
quick_slots_y = self.frame.y + 4
inventory_y = quick_slots_y + 42
inventory_height = (inventory_rows * (self.icon_size * 0.5)) + (inventory_rows * 3)
crafting_items_per_row = 2
crafting_y = inventory_y + inventory_height + 42
crafting_x = self.frame.x + 165
crafting_height = (crafting_rows * (self.icon_size * 0.5)) + (crafting_rows * 3)
crafting_width = (crafting_items_per_row * (self.icon_size * 0.5)) + (crafting_items_per_row-1) * 3
crafting_outcome_y = inventory_y + inventory_height + 60
crafting_outcome_x = self.frame.x + 270
crafting_outcome_width = crafting_outcome_height = self.icon_size * 0.5
# out of bound
if (x <= self.frame.x + 7) or (x >= (self.frame.x + self.frame.width) - 7) or (y <= quick_slots_y) or y >= (crafting_y + crafting_height):
return -1, -1
x_offset = x - (self.frame.x + 7)
if y <= quick_slots_y + 35:
row = 0.0
inventory = self.player.quick_slots
items_per_row = 9
elif y <= inventory_y + inventory_height and y >= inventory_y:
y_offset = (y - (inventory_y + inventory_height)) * -1
row = floor(y_offset // (self.icon_size * 0.5 + 3))
self.crafting_panel.remove_unnecessary_stacks()
inventory = self.player.inventory
items_per_row = 9
elif crafting_y <= y <= crafting_y + crafting_height and x >= crafting_x \
and x <= crafting_x + crafting_width:
y_offset = (y - (crafting_y + crafting_height)) * -1
row = floor(y_offset // (self.icon_size * 0.5 + 3))
self.crafting_panel.remove_unnecessary_stacks()
inventory = self.crafting_panel
x_offset = x - crafting_x
items_per_row = crafting_items_per_row
elif crafting_outcome_y <= y <= crafting_outcome_y + crafting_outcome_height and \
crafting_outcome_x <= x <= crafting_outcome_x + crafting_outcome_width:
return 0, 256 # 256 for crafting outcome
else:
return -1, -1
col = x_offset // (self.icon_size * 0.5 + 3)
return inventory, int(row * items_per_row + col)
def set_crafting_outcome(self, item):
if not item:
self.remove_crafting_outcome()
return
self.crafting_outcome = item
block = BLOCKS_DIR[item.type]
block_icon = self.get_block_icon(block)
self.crafting_outcome_icon = pyglet.sprite.Sprite(item_icon, batch=self.batch, group=self.group)
inventory_rows = floor(self.max_items / 9)
inventory_height = (inventory_rows * (self.icon_size * 0.5)) + (inventory_rows * 3)
quick_slots_y = self.frame.y + 4
inventory_y = quick_slots_y + 42
self.crafting_outcome_icon.scale = 0.5
self.crafting_outcome_icon.y = inventory_y + inventory_height + 60
self.crafting_outcome_icon.x = self.frame.x + 270
def remove_crafting_outcome(self):
self.crafting_outcome = None
self.crafting_outcome_icon = None
def set_selected_item(self, item):
if not item:
self.remove_selected_item()
return
self.selected_item = item
block = BLOCKS_DIR[item.type]
block_icon = self.get_block_icon(block)
self.selected_item_icon = pyglet.sprite.Sprite(block_icon, batch=self.batch, group=self.group)
self.selected_item_icon.scale = 0.4
def remove_selected_item(self):
self.selected_item = None
self.selected_item_icon = None
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
if self.visible:
return pyglet.event.EVENT_HANDLED
def on_mouse_press(self, x, y, button, modifiers):
if not self.visible:
return False
if x < 0.0 or y < 0.0:
return pyglet.event.EVENT_HANDLED
inventory, index = self.mouse_coords_to_index(x, y)
if index == 256: # 256 for crafting outcome
if self.crafting_outcome:
self.remove_selected_item()
# set selected_item to the crafting outcome so that users can put it in inventory
self.set_selected_item(self.crafting_outcome)
# set coordinates
inventory_rows = floor(self.max_items / 9)
inventory_height = (inventory_rows * (self.icon_size * 0.5)) + (inventory_rows * 3)
quick_slots_y = self.frame.y + 4
inventory_y = quick_slots_y + 42
self.selected_item_icon.y = inventory_y + inventory_height + 60
self.selected_item_icon.x = self.frame.x + 270
# cost
for ingre in self.crafting_panel.slots:
if ingre :
ingre.change_amount(-1)
# ingredient has been used up
if ingre.amount <= 0:
self.remove_crafting_outcome()
self.crafting_panel.remove_unnecessary_stacks()
return pyglet.event.EVENT_HANDLED
else: # nothing happens
return pyglet.event.EVENT_HANDLED
if self.selected_item:
if index == -1:
# throw it
self.update_items()
return pyglet.event.EVENT_HANDLED
item = inventory.at(index)
if (item and item.type == self.selected_item.type) or not item:
amount_to_change = 1
if button != pyglet.window.mouse.RIGHT:
amount_to_change = self.selected_item.amount
if item:
remaining = item.change_amount(amount_to_change)
else:
inventory.slots[index] = ItemStack(type=self.selected_item.type, amount=amount_to_change)
remaining = self.selected_item.amount - amount_to_change
if remaining > 0:
self.selected_item.change_amount((self.selected_item.amount - remaining) * -1)
else:
self.set_selected_item(None)
self.update_items()
return pyglet.event.EVENT_HANDLED
inventory.slots[index] = self.selected_item
self.set_selected_item(item)
if self.selected_item_icon:
self.selected_item_icon.x = x - (self.selected_item_icon.width / 2)
self.selected_item_icon.y = y - (self.selected_item_icon.height / 2)
else:
if index == -1:
return pyglet.event.EVENT_HANDLED
item = inventory.at(index)
if not item:
return pyglet.event.EVENT_HANDLED
if modifiers & pyglet.window.key.MOD_SHIFT:
add_to = self.player.quick_slots if inventory == self.player.inventory else self.player.inventory
add_to.add_item(item.type, item.amount)
inventory.remove_all_by_index(index)
self.update_items()
return pyglet.event.EVENT_HANDLED
new_stack = False
if button == pyglet.window.mouse.RIGHT:
if item.amount > 1:
split_amount = int(ceil(item.amount / 2))
item.change_amount(split_amount * -1)
new_item = ItemStack(item.type, split_amount, item.durability, item.data)
self.set_selected_item(new_item)
new_stack = True
if not new_stack:
self.set_selected_item(item)
if self.selected_item_icon:
self.selected_item_icon.x = x - (self.selected_item_icon.width / 2)
self.selected_item_icon.y = y - (self.selected_item_icon.height / 2)
if not new_stack:
inventory.remove_all_by_index(index)
self.update_items()
self.update_current()
return pyglet.event.EVENT_HANDLED
def on_mouse_motion(self, x, y, dx, dy):
if self.visible:
if self.selected_item_icon:
self.selected_item_icon.x = x - (self.selected_item_icon.width / 2)
self.selected_item_icon.y = y - (self.selected_item_icon.height / 2)
return pyglet.event.EVENT_HANDLED
def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
if self.visible:
if button == pyglet.window.mouse.LEFT:
self.on_mouse_motion(x, y, dx, dy)
return pyglet.event.EVENT_HANDLED
def on_key_press(self, symbol, modifiers):
if self.visible:
if symbol == key.ESCAPE:
self.toggle()
return pyglet.event.EVENT_HANDLED
elif symbol == key.ENTER:
return pyglet.event.EVENT_HANDLED
def on_resize(self, width, height):
self.frame.x = (width - self.frame.width) / 2
self.frame.y = self.icon_size / 2 - 4
if self.visible:
self.update_current()
self.update_items()
def draw(self):
if self.visible:
self.batch.draw()
if self.selected_item_icon:
self.selected_item_icon.draw()
if self.crafting_outcome_icon:
self.crafting_outcome_icon.draw()