From 06bae3bfe406f0827eecd2e0a0034a9a4f77566c Mon Sep 17 00:00:00 2001 From: seisatsu Date: Mon, 10 Apr 2017 23:46:49 -0700 Subject: [PATCH] Support centering containers Also support centering within containers. For some reason text centers a little low, but this may be an issue with the font. There is a lot of duplicate code that can be cleaned up now. --- data/test/blue1.py | 3 ++- src/widgetmanager.py | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/data/test/blue1.py b/data/test/blue1.py index 9bd818f..92c350e 100644 --- a/data/test/blue1.py +++ b/data/test/blue1.py @@ -6,5 +6,6 @@ def setup(): Driftwood.vars["end_rumble"] = "true" Driftwood.script.call("libs/stdlib/viewport.py", "rumble", 10, 3, None) - Driftwood.widget.text("Test Text", "pf_arma_five.ttf", 20, container=None, + h = Driftwood.widget.container(x=-1, y=-1, width=80, height=80) + Driftwood.widget.text("Test Text", "pf_arma_five.ttf", 16, container=h, x=-1, y=-1, width=-1, height=-1, color="0000FFFF", active=True) diff --git a/src/widgetmanager.py b/src/widgetmanager.py index 911e607..7d219a8 100644 --- a/src/widgetmanager.py +++ b/src/widgetmanager.py @@ -127,14 +127,38 @@ def container(self, imagefile=None, container=None, x=0, y=0, width=0, height=0, self.widgets[self.__last_wid].width = width self.widgets[self.__last_wid].height = height - # Are we inside a container? - if container is not None: + # Center if not in a container. + if container is None: + if x == -1: + self.widgets[self.__last_wid].x = \ + (self.driftwood.window.logical_width // self.driftwood.config["window"]["zoom"]) // 2 - \ + self.widgets[self.__last_wid].width // 2 + self.widgets[self.__last_wid].realx = self.widgets[self.__last_wid].x + if y == -1: + self.widgets[self.__last_wid].y = \ + (self.driftwood.window.logical_height // self.driftwood.config["window"]["zoom"]) // 2 - \ + self.widgets[self.__last_wid].height // 2 + self.widgets[self.__last_wid].realy = self.widgets[self.__last_wid].y + + else: if self.widgets[container].type == "container": # It has a container. self.widgets[self.__last_wid].container = container self.widgets[container].contains.append(self.__last_wid) if self.widgets[container].realx and self.widgets[container].realy: # Set the adjusted x and y. - self.widgets[self.__last_wid].realx += self.widgets[container].realx - self.widgets[self.__last_wid].realy += self.widgets[container].realy + # Either center or place in a defined position. + if x == -1: + self.widgets[self.__last_wid].realx = self.widgets[container].realx + \ + self.widgets[container].width // 2 - \ + self.widgets[self.__last_wid].width // 2 + else: + self.widgets[self.__last_wid].realx += self.widgets[container].realx + + if y == -1: + self.widgets[self.__last_wid].realy = self.widgets[container].realy + \ + self.widgets[container].height // 2 - \ + self.widgets[self.__last_wid].height // 2 + else: + self.widgets[self.__last_wid].realy += self.widgets[container].realy else: # Fake container. self.driftwood.log.msg("WARNING", "Widget", "not a container", container) @@ -219,14 +243,14 @@ def text(self, contents, font, ptsize, container=None, x=0, y=0, width=-1, heigh if x == -1: self.widgets[self.__last_wid].realx = self.widgets[container].realx + \ self.widgets[container].width // 2 - \ - width // 2 + self.widgets[self.__last_wid].width // 2 else: self.widgets[self.__last_wid].realx += self.widgets[container].realx if y == -1: self.widgets[self.__last_wid].realy = self.widgets[container].realy + \ self.widgets[container].height // 2 - \ - height // 2 + self.widgets[self.__last_wid].height // 2 else: self.widgets[self.__last_wid].realy += self.widgets[container].realy