forked from fogleman/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
186 lines (151 loc) · 9.79 KB
/
views.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
# Imports, sorted alphabetically.
# Python packages
# Nothing for now...
# Third-party packages
# Nothing for now...
# Modules from this project
import globals as G
from gui import *
from savingsystem import *
class View(pyglet.event.EventDispatcher):
def __init__(self, controller):
super(View, self).__init__()
self.controller = controller
self.batch = pyglet.graphics.Batch()
def setup(self):
pass
def add_handlers(self):
self.setup()
self.controller.window.push_handlers(self)
def pop_handlers(self):
self.controller.window.set_mouse_cursor(None)
self.controller.window.pop_handlers()
def update(self, dt):
pass
def clear(self):
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
def on_mouse_press(self, x, y, button, modifiers):
self.dispatch_event('on_mouse_click', x, y, button, modifiers)
def on_mouse_motion(self, x, y, dx, dy):
cursor = None
for button in self.buttons:
if button.highlighted:
button.highlighted = False
button.draw()
if button.hit_test(x, y):
button.highlighted = True
button.draw()
cursor = self.controller.window.get_system_mouse_cursor(pyglet.window.Window.CURSOR_HAND)
self.controller.window.set_mouse_cursor(cursor)
def on_draw(self):
self.clear()
glColor3d(1, 1, 1)
self.controller.set_2d()
self.batch.draw()
View.register_event_type('on_mouse_click')
class MainMenuView(View):
def setup(self):
self.group = pyglet.graphics.OrderedGroup(2)
self.labels_group = pyglet.graphics.OrderedGroup(3)
width, height = self.controller.window.width, self.controller.window.height
background = load_image('resources', 'textures', 'main_menu_background.png')
image = load_image('resources', 'textures', 'frame.png')
self.frame_rect = Rectangle(0, 0, image.width, image.height)
self.background = image_sprite(background, self.batch, 0)
self.background.scale = max(float(self.controller.window.get_size()[0]) / self.background.width, float(self.controller.window.get_size()[1]) / self.background.height)
self.frame = image_sprite(image, self.batch, 1)
button_image = load_image('resources', 'textures', 'button.png')
button_highlighted = load_image('resources', 'textures', 'button_highlighted.png')
# open_world(self, G.game_dir, G.SAVE_FILENAME)
self.buttons = []
if G.DISABLE_SAVE \
and world_exists(G.game_dir, G.SAVE_FILENAME):
self.continue_game = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Continue...", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.continue_game.push_handlers(on_click=self.controller.start_game_func)
self.buttons.append(self.continue_game)
self.new_game = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="New game", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.new_game.push_handlers(on_click=self.controller.new_game_func)
self.buttons.append(self.new_game)
self.game_options = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Options...", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.game_options.push_handlers(on_click=self.controller.game_options_func)
self.buttons.append(self.game_options)
self.exit_game = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Exit game", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.exit_game.push_handlers(on_click=self.controller.exit_game_func)
self.buttons.append(self.exit_game)
self.label = Label(G.APP_NAME, font_name='ChunkFive Roman', font_size=50, x=width/2, y=self.frame.y + self.frame.height,
anchor_x='center', anchor_y='top', color=(255, 255, 255, 255), batch=self.batch,
group=self.labels_group)
self.on_resize(width, height)
def on_resize(self, width, height):
self.background.scale = 1.0
self.background.scale = max(float(width) / self.background.width, float(height) / self.background.height)
self.background.x, self.background.y = 0, 0
self.frame.x, self.frame.y = (width - self.frame.width) / 2, (height - self.frame.height) / 2
self.label.y = self.frame.y + self.frame.height - 55
self.label.x = width / 2
button_x, button_y = 0, self.frame.y + (self.frame.height) / 2 + 10
for button in self.buttons:
button_x = self.frame.x + (self.frame.width - button.width) / 2
button.position = button_x, button_y
button_y -= button.height + 10
class OptionsView(View):
def setup(self):
self.group = pyglet.graphics.OrderedGroup(2)
self.labels_group = pyglet.graphics.OrderedGroup(3)
width, height = self.controller.window.width, self.controller.window.height
background = load_image('resources', 'textures', 'main_menu_background.png')
image = load_image('resources', 'textures', 'frame.png')
self.frame_rect = Rectangle(0, 0, image.width, image.height)
self.background = image_sprite(background, self.batch, 0)
self.background.scale = max(float(self.controller.window.get_size()[0]) / self.background.width, float(self.controller.window.get_size()[1]) / self.background.height)
self.frame = image_sprite(image, self.batch, 1)
button_image = load_image('resources', 'textures', 'button.png')
button_highlighted = load_image('resources', 'textures', 'button_highlighted.png')
self.button_return = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Done", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.button_return.push_handlers(on_click=self.controller.main_menu_func)
self.controls_button = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Controls...", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.controls_button.push_handlers(on_click=self.controller.controls_func)
self.buttons = [self.controls_button, self.button_return]
self.on_resize(width, height)
def on_resize(self, width, height):
self.background.scale = 1.0
self.background.scale = max(float(width) / self.background.width, float(height) / self.background.height)
self.background.x, self.background.y = 0, 0
self.frame.x, self.frame.y = (width - self.frame.width) / 2, (height - self.frame.height) / 2
button_x = self.frame.x + (self.frame.width - self.controls_button.width) / 2
button_y = self.frame.y + (self.frame.height - self.controls_button.height) / 2 + 10
self.controls_button.position = button_x, button_y
button_y -= self.controls_button.height + 20
self.button_return.position = button_x, button_y
class ControlsView(View):
def setup(self):
self.group = pyglet.graphics.OrderedGroup(2)
self.labels_group = pyglet.graphics.OrderedGroup(3)
width, height = self.controller.window.width, self.controller.window.height
background = load_image('resources', 'textures', 'main_menu_background.png')
image = load_image('resources', 'textures', 'frame.png')
self.frame_rect = Rectangle(0, 0, image.width, image.height)
self.background = image_sprite(background, self.batch, 0)
self.background.scale = max(float(self.controller.window.get_size()[0]) / self.background.width, float(self.controller.window.get_size()[1]) / self.background.height)
self.frame = image_sprite(image, self.batch, 1)
button_image = load_image('resources', 'textures', 'button.png')
button_highlighted = load_image('resources', 'textures', 'button_highlighted.png')
self.button_attack = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Attack", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.button_attack.push_handlers(on_click=self.capture_key_func)
self.button_return = Button(self, 0, 0, 160, 50, image=button_image, image_highlighted=button_highlighted, caption="Done", batch=self.batch, group=self.group, label_group=self.labels_group, font_name='ChunkFive Roman')
self.button_return.push_handlers(on_click=self.controller.game_options_func)
self.buttons = [self.button_attack, self.button_return]
self.on_resize(width, height)
def capture_key_func(self):
pass
def on_resize(self, width, height):
self.background.scale = 1.0
self.background.scale = max(float(width) / self.background.width, float(height) / self.background.height)
self.background.x, self.background.y = 0, 0
self.frame.x, self.frame.y = (width - self.frame.width) / 2, (height - self.frame.height) / 2
button_x = self.frame.x + (self.frame.width - self.button_attack.width) / 2
button_y = self.frame.y + (self.frame.height - self.button_attack.height) / 2 + 10
self.button_attack.position = button_x, button_y
button_y -= self.button_attack.height + 20
self.button_return.position = button_x, button_y