forked from fogleman/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 33
/
gui.py
1223 lines (1036 loc) · 48 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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Imports, sorted alphabetically.
# Python packages
import random
from math import floor, ceil
from functools import partial
# Third-party packages
import pyglet
from pyglet.gl import *
from pyglet.text import Label
from pyglet.window import key
from pyglet.image.atlas import TextureAtlas
# Modules from this project
from blocks import air_block
# FIXME: Initialize crafting in a proper way, other than by importing.
import crafting
import globals as G
from inventory import Inventory
from items import ItemStack
from utils import load_image, image_sprite, hidden_image_sprite, get_block_icon
__all__ = (
'Rectangle', 'Button', 'ToggleButton', 'Control', 'AbstractInventory',
'ItemSelector', 'InventorySelector', 'TextWidget', 'ProgressBarWidget',
'frame_image', 'button_image', 'button_highlighted', 'button_disabled',
'background_image', 'backdrop_images', 'rnd_backdrops', 'backdrop', 'ScrollbarWidget',
'resize_button_image'
)
class Rectangle:
def __init__(self, x, y, width, height):
self.position = x, y
self.size = width, height
def hit_test(self, x, y):
return (x >= self.min[0] and x <= self.max[0]) and (y >= self.min[1] and y <= self.max[1])
def vertex_list(self):
return [self.x, self.y,
self.x + self.width, self.y,
self.x + self.width, self.y + self.height,
self.x, self.y + self.height]
@property
def position(self):
return self.x, self.y
@position.setter
def position(self, position):
self.x, self.y = position
@property
def size(self):
return self.width, self.height
@size.setter
def size(self, size):
self.width, self.height = size
@property
def center(self):
return self.x + self.width // 2, self.y + self.height // 2
@property
def min(self):
return (self.x, self.y)
@property
def max(self):
return (self.x + self.width, self.y + self.height)
class Button(pyglet.event.EventDispatcher, Rectangle):
def __init__(self, parent, x, y, width, height, image=None, image_highlighted=None, caption=None, batch=None, group=None, label_group=None, font_name=G.DEFAULT_FONT, enabled=True):
super(Button, self).__init__(x, y, width, height)
parent.push_handlers(self)
self.batch, self.group, self.label_group = batch, group, label_group
self.sprite = image_sprite(image, self.batch, self.group)
self.sprite.scale = max(float(self.width) / float(image.width), float(self.height) / float(image.height))
self.sprite_highlighted = hidden_image_sprite(image_highlighted, self.batch, self.group)
self.sprite_highlighted.scale = max(float(self.width) / float(image_highlighted.width), float(self.height) / float(image_highlighted.height))
self.highlighted = False
self.label = Label(str(caption), font_name, 12, anchor_x='center', anchor_y='center',
color=(255, 255, 255, 255), batch=self.batch, group=self.label_group) if caption else None
self.position = x, y
self.enable(enabled)
def enable(self, enabled=True):
self.enabled = enabled
opacity = 255 if self.enabled else 100
if self.sprite:
self.sprite.opacity = opacity
if self.sprite_highlighted:
self.sprite_highlighted.opacity = opacity
if self.label:
self.label.color = (255, 255, 255, opacity)
def disable(self, enabled=False):
self.enable(enabled)
@property
def position(self):
return self.x, self.y
@position.setter
def position(self, position):
self.x, self.y = position
if hasattr(self, 'sprite') and self.sprite:
self.sprite.x, self.sprite.y = position
if hasattr(self, 'sprite_highlighted') and self.sprite_highlighted:
self.sprite_highlighted.x, self.sprite_highlighted.y = position
if hasattr(self, 'label') and self.label:
self.label.x, self.label.y = self.center
@property
def caption(self):
return self.label.text
@caption.setter
def caption(self, text):
self.label.text = text
def draw(self):
self.draw_sprite()
self.draw_label()
def draw_sprite(self):
if self.sprite and not (self.sprite_highlighted and self.highlighted):
self.sprite_highlighted.visible, self.sprite.visible = False, True
self.sprite.draw()
elif self.sprite_highlighted and self.highlighted:
self.sprite_highlighted.visible, self.sprite.visible = True, False
self.sprite_highlighted.draw()
def draw_label(self):
if self.label:
self.label.draw()
def on_mouse_click(self, x, y, button, modifiers):
if self.enabled and self.hit_test(x, y):
self.dispatch_event('on_click')
Button.register_event_type('on_click')
class ToggleButton(Button):
_toggled = False
@property
def toggled(self):
return self._toggled
@toggled.setter
def toggled(self, value):
self._toggled = value
if self.label:
if self._toggled:
color = (100, 0, 100, 255)
else:
color = (255, 255, 255, 255)
self.label.color = color
def __init__(self, parent, x, y, width, height, image=None, image_highlighted=None, caption=None, batch=None, group=None, label_group=None, font_name=G.DEFAULT_FONT, enabled=True):
super(ToggleButton, self).__init__(parent, x, y, width, height, image=image, image_highlighted=image_highlighted, caption=caption, batch=batch, group=group, label_group=label_group, font_name=font_name, enabled=enabled)
def on_mouse_click(self, x, y, button, modifiers):
if self.enabled and self.hit_test(x, y):
self.toggled = not self._toggled
self.dispatch_event('on_toggle')
super(ToggleButton, self).on_mouse_click( x, y, button, modifiers)
ToggleButton.register_event_type('on_toggle')
class Slot(pyglet.event.EventDispatcher, Rectangle):
_item = None
_hightlight = None
def __init__(self, parent, x, y, width, height, inventory=None, index=0, is_quickslot=False, world=None, batch=None, group=None, label_group=None):
super(Slot, self).__init__(x, y, width, height)
parent.push_handlers(self)
self.batch, self.group = batch, group
self.label_group = label_group
self.position = x, y
self.world = world
self.amount_label = None
self.is_quickslot = is_quickslot
self.icon = None
self.inventory = inventory
self.index = index
self.vertex_list = None
def __repr__(self):
return "Slot #" + str(self.index) + " in " + repr(self.inventory)
@property
def item(self):
return self._item
@item.setter
def item(self, value):
self._item = value
if self.amount_label:
self.amount_label.delete()
self.amount_label = None
if not value:
self.icon = None
return
img = get_block_icon(self._item.get_object(), self.width, self.world)
self.icon = image_sprite(img, self.batch, self.group)
image_scale = 1.0 / (img.width / self.width)
self.icon.scale = image_scale
self.icon.x = self.x
self.icon.y = self.y
if self.is_quickslot:
self._item.quickslots_x = self.icon.x
self._item.quickslots_y = self.icon.y
if self._item.max_durability != -1 and self._item.durability != -1:
self.icon.opacity = min(self._item.max_durability, self._item.durability + 1) * 255 // self._item.max_durability
if self._item.amount > 1:
self.amount_label = pyglet.text.Label(
str(self._item.amount), font_name=G.DEFAULT_FONT, font_size=9,
x=self.icon.x + 8, y=self.icon.y, anchor_x='left', anchor_y='bottom',
color=self._item.get_object().amount_label_color, batch=self.batch,
group=self.label_group)
@property
def highlighted(self):
return self._hightlight
@highlighted.setter
def highlighted(self, value):
if self.vertex_list:
self.vertex_list.delete()
self.vertex_list = None
if value:
self.vertex_list = self.batch.add(4, GL_QUADS, self.group,
('v2f', [self.x, self.y, self.x + self.width, self.y, self.x + self.width, self.y + self.height, self.x, self.y + self.height]),
('c4B', (255, 255, 255, 100) * 4))
def on_mouse_click(self, x, y, button, modifiers):
if self.hit_test(x, y):
self.dispatch_event('on_click')
Slot.register_event_type('on_click')
class Control(pyglet.event.EventDispatcher):
def __init__(self, parent, visible=True, *args, **kwargs):
self.parent = parent
self.visible = visible
self.focused = False
self.x, self.y, self.width, self.height = 0, 0, 0, 0
def toggle(self, state=None):
self.visible = not self.visible if state is None else state
self.focused = not self.focused if state is None else state
self._on_toggled()
self.dispatch_event('on_toggled')
def draw(self):
if self.visible:
self._on_draw()
def focus(self):
self.focused = True
def _on_toggled(self):
pass
def _on_draw(self):
pass
Control.register_event_type('on_toggled')
Control.register_event_type('key_released')
class AbstractInventory(Control):
def __init__(self, parent, *args, **kwargs):
super(AbstractInventory, self).__init__(parent, *args, **kwargs)
self._current_index = 0
self.batch = pyglet.graphics.Batch()
self.group = pyglet.graphics.OrderedGroup(1)
self.labels_group = pyglet.graphics.OrderedGroup(2)
@property
def current_index(self):
return int(self._current_index)
@current_index.setter
def current_index(self, value):
self._current_index = value % self.max_items
self.update_current()
def update_current(self):
pass
class ItemSelector(AbstractInventory):
def __init__(self, parent, player, world, *args, **kwargs):
super(ItemSelector, self).__init__(parent, *args, **kwargs)
self.world = world
self.player = player
self.max_items = 9
self.icon_size = 32
self.visible = True
self.num_keys = [getattr(G, 'INVENTORY_%d_KEY' % i)
for i in range(1, 10)]
image = G.texture_pack_list.selected_texture_pack.load_texture(['gui', 'gui.png'])
image_scale = image.height // 256
x_size = 182 * image_scale
y_size = 22 * image_scale
self.frame = image_sprite(image, self.batch, 0, y=image.height - y_size, height=y_size, x=0, width=x_size)
self.frame.scale = (1.0 / image_scale) * 2
self.frame.x = (self.parent.window.width - self.frame.width) // 2
heart_image = load_image('resources', 'gui', 'heart.png')
x_size = 24 * image_scale
y_size = 22 * image_scale
self.active = image_sprite(image, self.batch, 0, y=(image.height - (y_size + (22 * image_scale))), height=y_size, x=0, width=x_size)
self.active.scale = (1.0 / image_scale) * 2
self.slots = []
slot_x = self.frame.x + 8
slot_y = self.frame.y + 8
for i in range(1, self.max_items + 1):
self.slots.append(Slot(self, slot_x, slot_y, self.icon_size, self.icon_size, inventory=self.player.quick_slots, index=i-1, is_quickslot=True, world=self.world, batch=self.batch, group=self.group, label_group=self.labels_group))
slot_x += self.icon_size + 8
self.hearts = []
for i in range(0, 10):
heart = image_sprite(heart_image, self.batch, 0)
self.hearts.append(heart)
self.current_block_label = None
def update_items(self):
self.player.quick_slots.remove_unnecessary_stacks()
items = self.player.quick_slots.get_items()
items = items[:self.max_items]
i = 0
for item in items:
self.slots[i].item = item
i += 1
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=G.DEFAULT_FONT, 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 * 40) - 2
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.visible = True
if showed_hearts >= hearts_to_show:
heart.visible = False
showed_hearts += 1
def get_current_block(self):
item = self.player.quick_slots.at(self.current_index)
if not item:
return
return item.get_object()
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 _on_toggled(self):
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.current_index -= scroll_y
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.current_index = index
return pyglet.event.EVENT_HANDLED
elif symbol == G.VALIDATE_KEY:
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], durability=current_block[0].durability):
self.player.quick_slots.add_item(
current_block[0].id, quantity=current_block[1], durability=current_block[0].durability)
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 = 0
self.active.y = self.frame.y + 2
slot_x = self.frame.x + 8
slot_y = self.frame.y + 8
for slot in self.slots:
slot.x = slot_x
slot_x += self.icon_size + 8
if self.visible:
self.update_health()
self.update_current()
self.update_items()
def _on_draw(self):
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
self.batch.draw()
glDisable(GL_BLEND)
class InventorySelector(AbstractInventory):
def __init__(self, parent, player, world, *args, **kwargs):
super(InventorySelector, self).__init__(parent, *args, **kwargs)
# inv, crafting, furnace and common slots
self.batch = [ pyglet.graphics.Batch() ] * 4
self.group = [ pyglet.graphics.OrderedGroup(x) for x in range(1, 5) ]
self.parent = parent
self.world = world
self.player = player
self.max_items = self.player.inventory.slot_count
self.current_index = 1
self.icon_size = 32
self.selected_item = None
self.selected_item_icon = None
self.mode = 0 # 0 - Normal inventory, 1 - Crafting Table, 2 - Furnace
self.change_image()
self.crafting_panel = Inventory(4)
self.crafting_outcome = None # should be an item stack
self.crafting_table_panel = Inventory(9)
self.furnace_panel = None # should be a FurnaceBlock
self.visible = False
self.slots = None
# slots for inventory and crafting table
self.inv_slots = []
self.crafting_slots = []
self.furnace_slots = []
# inventory slots - common
# 9 items per row
rows = floor(self.max_items // 9)
inventory_y = 0
inventory_height = (rows * (self.icon_size + 8)) + ((rows+1) * 3)
slot_x = self.frame.x + 16
slot_y = self.frame.y + inventory_y + inventory_height
for i in range(1, self.max_items + 1):
slot = Slot(self, slot_x, slot_y, self.icon_size, self.icon_size, inventory=self.player.inventory, index=i-1, is_quickslot=False, world=self.world, batch=self.batch[3], group=self.group[3], label_group=self.labels_group)
slot_x += self.icon_size + 4
if slot_x >= (self.frame.x + self.frame.width) - 16:
slot_x = self.frame.x + 16
slot_y -= self.icon_size + 4
self.inv_slots.append(slot)
self.crafting_slots.append(slot)
self.furnace_slots.append(slot)
# quick slots - common
slot_x = self.frame.x + 16
slot_y = self.frame.y + 16
for i in range(1, self.player.quick_slots.slot_count + 1):
slot = Slot(self, slot_x, slot_y, self.icon_size, self.icon_size, inventory=self.player.quick_slots, index=i-1, is_quickslot=True, world=self.world, batch=self.batch[3], group=self.group[3], label_group=self.labels_group)
slot_x += self.icon_size + 4
self.inv_slots.append(slot)
self.crafting_slots.append(slot)
self.furnace_slots.append(slot)
# armor slots - inventory only
slot_x = self.frame.x + 16
slot_y = inventory_y + inventory_height + (4 * self.icon_size) - 10
for i in range(1, 4 + 1):
slot = Slot(self, slot_x, slot_y, self.icon_size, self.icon_size, inventory=self.player.armor, index=i-1, is_quickslot=False, world=self.world, batch=self.batch[0], group=self.group[0], label_group=self.labels_group)
slot_y += self.icon_size + 4
self.inv_slots.append(slot)
# crafting slots
# only for inventory and crafting table, furnace slots initialization is in self.set_furnace
for mode, panel, slots in [(0, self.crafting_panel, self.inv_slots),
(1, self.crafting_table_panel, self.crafting_slots)]:
crafting_rows = (2 if mode == 0 else 3 if mode == 1 else 2)
slot_x = self.frame.x + (176 if mode == 0 else 56 if mode == 1 else 63)
slot_y = self.frame.y + (inventory_y + inventory_height + (46 if mode == 0 else 25 if mode == 1 else 32)) + ((crafting_rows * self.icon_size) + (crafting_rows * 3))
self.current_panel = panel
for i in range(1, self.current_panel.slot_count + 1):
slot = Slot(self, slot_x, slot_y, self.icon_size, self.icon_size, inventory=self.current_panel, index=i-1, is_quickslot=False, world=self.world, batch=self.batch[mode], group=self.group[mode], label_group=self.labels_group)
slots.append(slot)
slot_x += self.icon_size + 4
if i%(2 if mode == 0 else 3 if mode == 1 else 1) == 0:
slot_x = self.frame.x + (176 if mode == 0 else 56 if mode == 1 else 63)
slot_y -= self.icon_size + 4
# crafting outcome slot
for mode, slots in [(0, self.inv_slots),
(1, self.crafting_slots),
(2, self.furnace_slots)]:
slot_x, slot_y = 0, 0
if mode == 0:
slot_x, slot_y = 288, 96
elif mode == 1:
slot_x, slot_y = 246, 64
elif mode == 2:
slot_x, slot_y = 222, 67
slot = Slot(self, self.frame.x + slot_x, self.frame.y + inventory_y + inventory_height + slot_y, self.icon_size, self.icon_size, inventory=0, index=256, is_quickslot=False, world=self.world, batch=self.batch[mode], group=self.group[mode], label_group=self.labels_group)
slots.append(slot)
self.switch_mode(0)
def switch_mode(self, mode=0, reset_mode=True):
self.mode = mode
self.change_image()
if mode == 0:
self.slots = self.inv_slots
self.current_panel = self.crafting_panel
if reset_mode: self.reset_furnace()
elif mode == 1:
self.current_panel = self.crafting_table_panel
self.slots = self.crafting_slots
elif mode == 2:
self.slots = self.furnace_slots
self.current_panel = self.furnace_panel
def change_image(self):
image = G.texture_pack_list.selected_texture_pack.load_texture(['gui', 'inventory.png' if self.mode == 0 else 'crafting.png' if self.mode == 1 else 'furnace.png'])
image_scale = image.height // 256
x_size = 176 * image_scale
y_size = 166 * image_scale
self.frame = image_sprite(image, self.batch[self.mode], 0, y=image.height - y_size, height=y_size, x=0, width=x_size)
self.frame.scale = (1.0 / image_scale) * 2
self.frame.x = (self.parent.window.width - self.frame.width) // 2
self.frame.y = 74
def update_items(self):
# only inventory has armor slots
items = self.player.inventory.get_items()[:self.max_items] + self.player.quick_slots.get_items()[:self.player.quick_slots.slot_count] + \
(self.player.armor.get_items()[:4] if self.mode == 0 else [])
i = 0
for item in items:
self.slots[i].item = item
i += 1
# NOTE: each line in the crafting panel should be a sub-list in the crafting ingredient list
crafting_ingredients = [[], [], []] if self.mode == 1 else [[], []]
items = self.current_panel.get_items()[:self.current_panel.slot_count]
for j, item in enumerate(items):
self.slots[i].item = item
if not item or item.get_object().id > 0:
crafting_ingredients[int(floor(j // (2 if self.mode == 0 else 3 if self.mode == 1 else 1)))].append(air_block if not item else item.get_object())
i += 1
if len(crafting_ingredients) > 0:
if self.mode < 2:
outcome = G.recipes.craft(crafting_ingredients)
self.set_crafting_outcome(outcome)
elif self.mode == 2:
outcome = self.furnace_panel.get_smelt_outcome()
self.set_crafting_outcome(outcome)
self.world.packetreceiver.send_player_inventory() # Tell the server.
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, reset_mode=True):
if not self.visible:
self.update_items()
if reset_mode:
self.mode = 0
self.switch_mode(self.mode, reset_mode)
self.parent.item_list.toggle()
self.parent.window.set_exclusive_mouse(self.visible)
self.visible = not self.visible
def set_furnace(self, furnace):
self.furnace_panel = furnace
crafting_rows = 2
rows = floor(self.max_items // 9)
inventory_y = 0
inventory_height = (rows * (self.icon_size + 8)) + ((rows+1) * 3)
slot_x = self.frame.x + 63
slot_y = self.frame.y + (inventory_y + inventory_height + 32) + ((crafting_rows * self.icon_size) + (crafting_rows * 3))
for i in range(1, self.furnace_panel.slot_count + 1):
slot = Slot(self, slot_x, slot_y, self.icon_size, self.icon_size, inventory=self.furnace_panel, index=i-1, is_quickslot=False, world=self.world, batch=self.batch[2], group=self.group[2], label_group=self.labels_group)
self.furnace_slots.append(slot)
slot_x += self.icon_size + 4
if i == 0:
slot_x = self.frame.x + 63
slot_y -= self.icon_size + 4
# install callback
self.furnace_panel.set_outcome_callback(self.update_items)
self.furnace_panel.set_fuel_callback(self.update_items)
def reset_furnace(self):
# remove callback
if self.furnace_panel is None:
return
self.furnace_panel.set_outcome_callback(None)
self.furnace_panel.set_fuel_callback(None)
self.furnace_panel = None
def set_crafting_outcome(self, item):
self.crafting_outcome = item
self.slots[-1].item = item
def set_selected_item(self, item, x=0, y=0):
if not item:
self.remove_selected_item()
return
self.selected_item = item
img = get_block_icon(item.get_object(), self.icon_size, self.world)
self.selected_item_icon = image_sprite(img, self.batch[self.mode], self.group[self.mode])
image_scale = 0.8 / (img.width / self.icon_size)
self.selected_item_icon.scale = image_scale
self.selected_item_icon.x = x - (self.selected_item_icon.width // 2)
self.selected_item_icon.y = y - (self.selected_item_icon.height // 2)
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 = None
index = -1
for slot in self.slots:
if slot.hit_test(x, y):
inventory = slot.inventory
index = slot.index
break
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, x, y)
# cost
for ingre in self.current_panel.slots:
if ingre :
ingre.change_amount(-1)
# ingredient has been used up
if ingre.amount <= 0:
self.set_crafting_outcome(None)
self.current_panel.remove_unnecessary_stacks()
self.update_items()
return pyglet.event.EVENT_HANDLED
if self.selected_item:
if index == -1:
# throw it
if not (self.frame.x <= x <= self.frame.x + self.frame.width and self.frame.y <= y <= self.frame.y + self.frame.height):
self.remove_selected_item()
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:
if hasattr(inventory, 'set_slot'):
inventory.set_slot(index, ItemStack(type=self.selected_item.type, durability=self.selected_item.durability, amount=amount_to_change))
else:
inventory.slots[index] = ItemStack(type=self.selected_item.type, durability=self.selected_item.durability, 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
if hasattr(inventory, 'set_slot'):
inventory.set_slot(index, self.selected_item)
else:
inventory.slots[index] = self.selected_item
self.set_selected_item(item, x, y)
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, durability=item.durability)
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)
self.set_selected_item(new_item, x, y)
new_stack = True
if not new_stack:
self.set_selected_item(item, x, y)
inventory.remove_all_by_index(index)
self.update_items()
return pyglet.event.EVENT_HANDLED
def on_mouse_motion(self, x, y, dx, dy):
if self.visible:
for slot in self.slots:
slot.highlighted = slot.hit_test(x, y)
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 == G.ESCAPE_KEY:
self.toggle()
return pyglet.event.EVENT_HANDLED
elif symbol == G.VALIDATE_KEY:
return pyglet.event.EVENT_HANDLED
def on_resize(self, width, height):
self.frame.x = (width - self.frame.width) // 2
self.frame.y = 74
if self.visible:
self.update_items()
def _on_draw(self):
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
self.batch[self.mode].draw()
self.batch[3].draw()
if self.selected_item_icon:
self.selected_item_icon.draw()
glDisable(GL_BLEND)
# TODO: This is a total hack. The issue seen here: https://code.google.com/p/pyglet/issues/detail?id=471
# Makes it impossible to set styles to a FormattedDocument (font family, font size, color, etc) because if the document
# text ever becomes empty, exceptions are thrown. There is a fix below from 2012 but it apparently does not exist
# in 1.1.4? So I apply it by rewriting the RunIterator.__getitem__ method.
# https://code.google.com/p/pyglet/source/diff?spec=svn64e3a450c83bd2245f047bb96fdacd79208d8b6a&r=64e3a450c83bd2245f047bb96fdacd79208d8b6a&format=side&path=/pyglet/text/runlist.py
def __run_iterator_fix(self, index):
while index >= self.end and index > self.start:
# condition has special case for 0-length run (fixes issue 471)
self.start, self.end, self.value = next(self)
return self.value
from pyglet.text.runlist import RunIterator
RunIterator.__getitem__ = __run_iterator_fix
class TextWidget(Control):
"""
Variation of this example: http://www.pyglet.org/doc/programming_guide/text_input.py
"""
batch: pyglet.graphics.Batch
x: int
y: int
width: int
height: int
def __init__(self, parent, text, x: int, y: int, width: int, height: int = None, multi_line=False,
font_size=12,
font_name=G.DEFAULT_FONT,
text_color=(0, 0, 0, 255),
background_color=(200, 200, 200, 128),
readonly=False,
batch = None,
enable_escape = False,
*args, **kwargs):
super(TextWidget, self).__init__(parent, *args, **kwargs)
self.batch = pyglet.graphics.Batch() if not batch else batch
self.vertex_list = None
blank_text = not bool(text)
self.document = pyglet.text.document.FormattedDocument(text if not blank_text else ' ')
self.default_style = dict(color=text_color,
font_size=font_size,
font_name=font_name)
# parse escape sequences
self.enable_escape = enable_escape
self.document.set_style(0, len(self.document.text), self.default_style)
font = self.document.get_font(0)
if blank_text:
self.clear()
self.padding = 10
self.x = x
self.y = y
self.width = width
self.height = height or (font.ascent - font.descent) + self.padding
self.multi_line = multi_line
self.background_color = background_color
self.layout = pyglet.text.layout.IncrementalTextLayout(
self.document, self.width, self.height, multiline=self.multi_line, batch=self.batch)
self.caret = pyglet.text.caret.Caret(self.layout)
self.caret.visible = not readonly
self.readonly = readonly
self.layout.x = x
self.layout.y = y
self.resize()
def focus(self):
super(TextWidget, self).focus()
self.caret.visible = True
self.caret.mark = 0
self.caret.position = len(self.document.text)
def hit_test(self, x, y):
return (0 < x - self.layout.x < self.layout.width and
0 < y - self.layout.y < self.layout.height)
@property
def text(self):
return self.document.text
@text.setter
def text(self, text):
self.document.text = text
def clear(self):
self.text = ''
def write(self, text, **kwargs):
"""
Write the text to the widget.
"""
start = len(self.text)
end = start + len(text)
self.document.insert_text(start, text)
self.document.set_style(start, end, kwargs)
if self.multi_line:
self.layout.view_y = -self.layout.content_height # Scroll to the bottom
def write_escape(self, text):
# parse escape sequences
# a escape sequence is a sub-sequence in the text which starts with '$$'
# currently available escape sequences:
# $$r: set text color to red
# $$g: set text color to green
# $$b: set text color to blue
# $$y: set text color to yellow
# $$m: set text color to magenta
# $$c: set text color to cyan
# $$D: set text style to default
style = self.default_style.copy()
cooked_text = ''
escape = 0
for c in text:
# command
if escape == 1:
if c == 'r':
style['color'] = [255, 0, 0, 255]
if c == 'g':
style['color'] = [0, 255, 0, 255]
if c == 'b':
style['color'] = [0, 0, 255, 255]
if c == 'y':
style['color'] = [255, 255, 0, 255]
if c == 'm':
style['color'] = [255, 0, 255, 255]
if c == 'c':
style['color'] = [0, 255, 255, 255]
if c == 'D':
style = self.default_style.copy()
escape = 0 # end sequence
continue
if c == '$':
# first '$' char
if escape == 0:
escape = -1
continue
# second '$' char
if escape == -1:
# output the text
self.write(cooked_text, **style)
cooked_text = ''
escape = 1
else:
cooked_text += c
if cooked_text != '':
self.write(cooked_text, **style)
def write_line(self, text, **kwargs):
"""
Write the text followed by a newline. Only effective if multi_line is True.
"""
if self.enable_escape:
self.write_escape("%s\n" % text)
else:
self.write("%s\n" % text, **kwargs)
def resize(self, x=None, y=None, width=None, height=None):
self.x = x or self.x
self.y = y or self.y
self.width = width or self.width
self.height = height or self.height
# Recreate the bounding box
self.rectangle = Rectangle(self.x - self.padding, self.y - self.padding,
self.width + self.padding, self.height + self.padding)
# And reposition the text layout
self.layout.x = self.x + self.padding
self.layout.y = (self.rectangle.y + (self.rectangle.height//2) - (self.height//2))
self.layout.width = self.rectangle.width - self.padding
self.layout.height = self.rectangle.height - self.padding
if self.vertex_list:
self.vertex_list.delete()
self.vertex_list = self.batch.add(4, pyglet.gl.GL_QUADS, None,
('v2i', self.rectangle.vertex_list()),
('c4B', self.background_color * 4)
)
def _on_draw(self):
self.batch.draw()
def _on_toggled(self):
self.parent.set_exclusive_mouse(not self.visible)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.visible:
self.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
return pyglet.event.EVENT_HANDLED
def on_text(self, text):
if self.visible:
if not self.multi_line:
text = text.replace('\r', '') # Remove carriage returns
self.caret.on_text(text)
return pyglet.event.EVENT_HANDLED
def on_text_motion(self, motion):
if self.visible:
self.caret.on_text_motion(motion)
return pyglet.event.EVENT_HANDLED