Esempio n. 1
0
class EventLogController(pygsty.controllers.BaseController):
    def __init__(self):
        super().__init__()
        self.hud_batch = pyglet.graphics.Batch()
        self.doc = Document(self.last_message, width=800, height=100, is_fixed_size=True)
        self.frame = Frame(self.doc)
        self.manager = Manager(self.frame, window=pygsty.engine(), batch=self.hud_batch, theme=config.ui_theme)
        self.manager.set_position(0, 0)
        self._latest_event = None
        self._log = self.last_message

    def draw_hud(self):
        self.hud_batch.draw()

    @property
    def last_message(self):
        last_event = models.event_log.last_event()
        if last_event:
            return last_event.formatted_message()
        else:
            return ""

    def update(self, dt):
        if not models.event_log.last_event() is self._latest_event:
            self._log = self.last_message + self._log
            self.doc.set_text(self._log)
            self._latest_event = models.event_log.last_event()
Esempio n. 2
0
		return True

def f(value):
	print(value)
	global GROWTH
	if value <= 2:
		H.zoom_mode = False
	else:
		H.zoom_mode = True
		GROWTH = 100 - value

L = Letters(text=letters)
HS = HorizontalSlider(value=12, min_value=1.0, max_value=99.9, width=150, on_set=f)

Manager(VerticalContainer([HS, L]), window=window, batch=batch, theme=theme)
Manager.set_position(L, x=SCREENSIZE-300, y=SCREENSIZE-50)
Manager.set_position(HS, x=SCREENSIZE-300, y=SCREENSIZE-100)
# @clock
def drawArray(someArray):
	vertPoints = someArray[:, :2].flatten().astype(ctypes.c_float)
	gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertPoints.ctypes.data)
	gl.glDrawArrays(gl.GL_POINTS, 0, len(vertPoints) // 2)

@window.event
def on_draw():
	gl.glPointSize(H.pointsize)
	gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
	drawArray(H.field)

def update(dt):
	window.clear()
Esempio n. 3
0
class HUD:

	def __init__(self, window):
		with open('etc/gui/theme.json') as theme_file:
			theme_dict = json.loads(theme_file.read())
		self.theme = Theme(
			theme_dict,
			resources_path=os.path.join(os.getcwd(), 'etc/gui/images')
			)
		self.window = window
		self.clear_action_gui()
		self.clear_status_gui()

	def handle_click(self, button_state, action):
		print(action)
		if button_state is not False:
			self.selected_action = action
		return True

	def handle_building_click(self, button_state, button, selected_object, action):
		if button_state is not False:
			self.selected_action = action
			selected_object.add_action(action) #this should probably get moved to the event_handler
		return True

	def clear_action_gui(self):
		self.selected_action = None
		self.action_gui_batch = None
		self.action_gui_manager = None

	def clear_status_gui(self):
		self.status_box = None
		self.status_gui_batch = None
		self.status_gui_manager = None

	def build_building_gui(self, selected_building):
		building = selected_building
		produce_content = [Label(text='Produce')]
		for resource in building.producable_resources:
			produce_button = GroupButton(
				group_id='action-gui-buttons',
				label=resource.name,
				on_press=lambda x: self.handle_building_click(x, produce_button, building, Produce(building, resource, selected_building.container.remaining_capacity(resource))),
				)
			produce_content.append(produce_button)
		produce_container = HorizontalContainer(produce_content)

		action_container = VerticalContainer([
			produce_container,
			])
		self.action_gui_batch = pyglet.graphics.Batch()
		self.action_gui_manager = Manager(
			action_container,
			window=self.window,
			theme=self.theme,
			batch=self.action_gui_batch,
			)
		self.action_gui_manager.set_position(
			#it's on the right as a hack to fix the Manager bug
			self.window.width - action_container.width - 30,
			self.window.height - action_container.height - 10,
			)
		self.action_gui_manager.is_movable = False

	def build_unit_gui(self, selected_object):
		harvest_content = [Label(text='Harvest')]
		#for resource in selected_object.harvestable_resources:
		#	harvest_button = GroupButton(
		#		group_id='action-gui-buttons',
		#		label=resource.name,
		#		on_press=lambda x: self.handle_click(x, Harvest(resource, selected_object.container.remaining_capacity(resource))),
		#		)
		#	harvest_content.append(harvest_button)
		harvest_wood_button = GroupButton(
			group_id='action-gui-buttons',
			label=Wood.name,
			on_press=lambda x: self.handle_click(x, Harvest(Wood, selected_object.container.remaining_capacity(Wood))),
			)
		harvest_content.append(harvest_wood_button)
		harvest_container = HorizontalContainer(harvest_content)

		building_content = [Label(text='Construct')]
		#for building_factory in selected_object.building_factories.values():
		#	building_button = GroupButton(
		#		group_id='action-gui-buttons',
		#		label=building_factory.product.name,
		#		on_press=lambda x:
		#			self.handle_click(x, Construct(building_factory.product)),
		#		)
		#	building_content.append(building_button)
		building_cabbage_farm_button = GroupButton(
			group_id='action-gui-buttons',
			label=CabbageFarm.name,
			on_press=lambda x:
				self.handle_click(x, Construct(CabbageFarm)),
			)
		building_content.append(building_cabbage_farm_button)
		building_iron_mine_button = GroupButton(
			group_id='action-gui-buttons',
			label=IronMine.name,
			on_press=lambda x:
				self.handle_click(x, Construct(IronMine)),
			)
		building_content.append(building_iron_mine_button)
		#building_farm_button = GroupButton(
		#	group_id='action-gui-buttons',
		#	label=building_factory.Farm.name,
		#	on_press=lambda x:
		#		self.handle_click(x, Construct(building_factory.Farm)),
		#	)
		#building_content.append(building_farm_button)
		construct_container = HorizontalContainer(building_content)

		deliver_content = [Label(text='Deliver')]
		#for resource in selected_object.carryable_resources:
		#	deliver_button = GroupButton(
		#		group_id='action-gui-buttons',
		#		label=resource.name,
		#		on_press=lambda x: self.handle_click(x, Deliver(resource, selected_object.container.current_load(resource))),
		#		)
		#	deliver_content.append(deliver_button)
		#
		#For some reason adding the buttons in a loop causes them to get the same action (same resource)
		#TODO change the interface so that only relevant resource buttons appear
		deliver_wood_button = GroupButton(
			group_id='action-gui-buttons',
			label=Wood.name,
			on_press=lambda x: self.handle_click(x, Deliver(Wood, selected_object.container.current_load(Wood))),
			)
		deliver_content.append(deliver_wood_button)
		deliver_lumber_button = GroupButton(
			group_id='action-gui-buttons',
			label=Lumber.name,
			on_press=lambda x: self.handle_click(x, Deliver(Lumber, selected_object.container.current_load(Lumber))),
			)
		deliver_content.append(deliver_lumber_button)
		deliver_container = HorizontalContainer(deliver_content)

		deliver_all_content = [Label(text='DeliverAll')]
		deliver_all_button = GroupButton(
			group_id='action-gui-buttons',
			label='all',
			on_press=lambda x: self.handle_click(x, DeliverAll(selected_object)
			))
		deliver_all_content.append(deliver_all_button)
		deliver_all_container = HorizontalContainer(deliver_all_content)

		pickup_content = [Label(text='Pick up')]
		#for resource in selected_object.carryable_resources:
		#	pickup_button = GroupButton(
		#		group_id='action-gui-buttons',
		#		label=resource.name,
		#		on_press=lambda x: self.handle_click(x, Pickup(resource, selected_object.container.remaining_capacity(resource))),
		#		)
		#	pickup_content.append(pickup_button)
		#pickup_container = HorizontalContainer(pickup_content)
		#
		#For some reason adding the buttons in a loop causes them to get the same action (same resource)
		#TODO change the interface so that only relevant resource buttons appear
		pickup_wood_button = GroupButton(
			group_id='action-gui-buttons',
			label=Wood.name,
			on_press=lambda x: self.handle_click(x, Pickup(Wood, selected_object.container.remaining_capacity(Wood))),
			)
		pickup_content.append(pickup_wood_button)
		pickup_lumber_button = GroupButton(
			group_id='action-gui-buttons',
			label=Lumber.name,
			on_press=lambda x: self.handle_click(x, Pickup(Lumber, selected_object.container.remaining_capacity(Lumber))),
			)
		pickup_content.append(pickup_lumber_button)
		pickup_container = HorizontalContainer(pickup_content)

		pickup_all_content = [Label(text='PickupAll')]
		pickup_all_button = GroupButton(
			group_id='action-gui-buttons',
			label='all',
			on_press=lambda x: self.handle_click(x, PickupAll(selected_object)
			))
		pickup_all_content.append(pickup_all_button)
		pickup_all_container = HorizontalContainer(pickup_all_content)

		action_container = VerticalContainer([
			harvest_container,
			construct_container,
			deliver_container,
			deliver_all_container,
			pickup_container,
			pickup_all_container
			])
		self.action_gui_batch = pyglet.graphics.Batch()
		self.action_gui_manager = Manager(
			action_container,
			window=self.window,
			theme=self.theme,
			batch=self.action_gui_batch,
			)
		self.action_gui_manager.set_position(
			10,
			self.window.height - action_container.height - 10,
			)
		self.action_gui_manager.is_movable = False

	def build_action_gui(self, selected_object):
		if selected_object is None:
			raise HUDException("Can't build action gui with a unit of None")

		if isinstance(selected_object, Building):
			self.build_building_gui(selected_object)

		if isinstance(selected_object, Unit):
			self.build_unit_gui(selected_object)

	def build_status_gui(self):
		self.status_box = Document(
			pyglet.text.document.UnformattedDocument(),
			width=self.window.width*0.9,
			height=self.window.height/4,
			is_fixed_size=True,
			)
		status_frame = Frame(self.status_box)
		self.status_gui_batch = pyglet.graphics.Batch()
		self.status_gui_manager = Manager(
			status_frame,
			window=self.window,
			theme=self.theme,
			batch=self.status_gui_batch,
			)
		self.status_gui_manager.set_position(
			(self.window.width - status_frame.width)/2,
			10,
			)
		self.status_gui_manager.is_movable = False

	def draw(self, selected_unit):
		glLoadIdentity()
		gluOrtho2D(0, self.window.width, 0, self.window.height)
		if selected_unit is not None:
			if self.action_gui_batch is None:
				self.build_action_gui(selected_unit)
			if self.action_gui_batch is not None:
				self.action_gui_batch.draw()
			if self.status_gui_batch is None:
				self.build_status_gui()
			self.status_box.set_text(selected_unit.status)
			self.status_gui_batch.draw()