Skip to content

Commit

Permalink
Support centering containers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seisatsu committed Apr 11, 2017
1 parent 18bc214 commit 06bae3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion data/test/blue1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 30 additions & 6 deletions src/widgetmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 06bae3b

Please sign in to comment.