Mercurial > fife-parpg
changeset 616:c770794d0a4a
Added a simple percentage bar widget along with a quick demo in the sliders portion of the pychan_demo. Note: I have not tested the foreground image feature yet. fixes[t:411]
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 27 Sep 2010 15:38:09 +0000 |
parents | 6f3f5686a56e |
children | 69ef061db940 |
files | demos/pychan_demo/gui/slider.xml demos/pychan_demo/sliders.py engine/core/gui/widgets/percentagebar.cpp engine/core/gui/widgets/percentagebar.hpp engine/core/gui/widgets/widgets.i engine/python/fife/extensions/pychan/widgets/__init__.py engine/python/fife/extensions/pychan/widgets/percentagebar.py |
diffstat | 7 files changed, 333 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/demos/pychan_demo/gui/slider.xml Mon Sep 27 14:22:07 2010 +0000 +++ b/demos/pychan_demo/gui/slider.xml Mon Sep 27 15:38:09 2010 +0000 @@ -7,5 +7,8 @@ <Slider name="xslider" scale_start="0" scale_end="300"/> <HBox><Label hexpand="0" text="Y:"/><Label name="yvalue"/></HBox> <Slider name="yslider" scale_start="0" scale_end="300"/> + <HBox><Label hexpand="0" text="Percentage Bar:"/><Label name="pvalue"/></HBox> + <Slider name="pbarslider" scale_start="0" scale_end="100"/> + <PercentageBar name="pbar" orientation="0" max_size="300,10" min_size="300,10" foreground_color="128,0,0"/> <Button name="closeButton" text="Close"/> </Window>
--- a/demos/pychan_demo/sliders.py Mon Sep 27 14:22:07 2010 +0000 +++ b/demos/pychan_demo/sliders.py Mon Sep 27 15:38:09 2010 +0000 @@ -32,6 +32,7 @@ self.widget.mapEvents({ 'xslider': self.update, 'yslider': self.update, + 'pbarslider' : self.update, 'closeButton':self.stop, }) self.update() @@ -50,3 +51,9 @@ 'xvalue' : unicode(icon.x), 'yvalue' : unicode(icon.y), }) + + #quick demo to show the percentage bar in action + pbarslider = self.widget.findChild(name="pbarslider") + pbar = self.widget.findChild(name="pbar") + + pbar.value = int(pbarslider.getValue())
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine/core/gui/widgets/percentagebar.cpp Mon Sep 27 15:38:09 2010 +0000 @@ -0,0 +1,108 @@ +/*************************************************************************** + * Copyright (C) 2005-2010 by the FIFE team * + * http://www.fifengine.net * + * This file is part of FIFE. * + * * + * FIFE is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +/* + * For comments regarding functions please see the header file. + */ + +// Standard C++ library includes + +// 3rd party library includes +#include <guichan/rectangle.hpp> +#include <guichan/graphics.hpp> + +// FIFE includes +// These includes are split up in two parts, separated by one empty line +// First block: files included from the FIFE root src directory +// Second block: files included from the same folder +#include "percentagebar.hpp" + +namespace gcn +{ + + PercentageBar::PercentageBar() + { + mImage = 0; + + setOrientation(HORIZONTAL); + setValue(0); + } + + void PercentageBar::draw(Graphics* graphics) + { + graphics->setColor(getForegroundColor()); + + if (getOrientation() == HORIZONTAL) + { + graphics->fillRectangle(gcn::Rectangle(0,0,getWidth() * mValue/100,getHeight())); + } + else + { + graphics->fillRectangle(gcn::Rectangle(0,getHeight()-getHeight() * mValue/100,getWidth(),getHeight() * mValue/100)); + } + + + if ( mImage ) + graphics->drawImage(mImage, 0, 0); + + } + + void PercentageBar::setForegroundImage(Image* image) + { + mImage = image; + if( mImage ) { + setHeight(image->getHeight()); + setWidth(image->getWidth()); + } + } + + void PercentageBar::setValue(int value) + { + if (value > 100) + { + mValue = 100; + return; + } + + if (value < 0) + { + mValue = 0; + return; + } + + mValue = value; + } + + int PercentageBar::getValue() const + { + return mValue; + } + + void PercentageBar::setOrientation(PercentageBar::Orientation orientation) + { + mOrientation = orientation; + } + + PercentageBar::Orientation PercentageBar::getOrientation() const + { + return mOrientation; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine/core/gui/widgets/percentagebar.hpp Mon Sep 27 15:38:09 2010 +0000 @@ -0,0 +1,120 @@ +/*************************************************************************** + * Copyright (C) 2005-2010 by the FIFE team * + * http://www.fifengine.net * + * This file is part of FIFE. * + * * + * FIFE is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef GCN_PERCENTAGE_BAR_HPP +#define GCN_PERCENTAGE_BAR_HPP + +// Standard C++ library includes + +// 3rd party library includes +#include <guichan/image.hpp> +#include <guichan/platform.hpp> +#include <guichan/widget.hpp> + +// FIFE includes +// These includes are split up in two parts, separated by one empty line +// First block: files included from the FIFE root src directory +// Second block: files included from the same folder + +namespace gcn +{ + /** + * A simple percentage bar. + * + * + */ + class PercentageBar : public Widget + { + public: + + enum Orientation + { + HORIZONTAL = 0, + VERTICAL + }; + + /** + * Constructor. + * + */ + PercentageBar(); + virtual ~PercentageBar() {} + + + // Inherited from Widget + + virtual void draw(Graphics* graphics); + + /* + * Sets the image that will be displayed above the widget + * + * @param image Image to be displayed + */ + void setForegroundImage(Image* image); + + /** + * Sets the orientation of the percentage bar. + * + * @param orientation The orientation of the percentage bar. + * @see getOrientation + */ + void setOrientation(Orientation orientation); + + /** + * Gets the orientation of the percentage bar.. + * + * @return The orientation of the percentage bar. + * @see setOrientation + */ + Orientation getOrientation() const; + + /** + * Gets the value of the percentage bar + * + * @return The value of the percentage bar. + */ + int getValue() const; + + /** + * Sets the value of the percentage bar + * + * @param value In the range of 0-100. + */ + void setValue(int value); + + // changed from private to allow derived instance access + protected: + Image* mImage; + + /** + * Holds the current value of the percentage bar. + */ + double mValue; + + /** + * Holds the orientation of the percentage bar. A percentage bar can be drawn + * vertically or horizontally. + */ + Orientation mOrientation; + }; +} + +#endif // end GCN_PERCENTAGE_BAR_HPP
--- a/engine/core/gui/widgets/widgets.i Mon Sep 27 14:22:07 2010 +0000 +++ b/engine/core/gui/widgets/widgets.i Mon Sep 27 15:38:09 2010 +0000 @@ -29,6 +29,7 @@ #include "gui/widgets/togglebutton.h" #include "gui/widgets/clicklabel.h" #include "gui/widgets/icon2.hpp" +#include "gui/widgets/percentagebar.hpp" %} namespace gcn { @@ -422,6 +423,24 @@ virtual ~Icon2(); void setImage(Image* image); }; + + %feature("notabstract") PercentageBar; + class PercentageBar: public Widget { + public: + PercentageBar(); + virtual ~PercentageBar(); + virtual void setForegroundImage(Image* image); + virtual void setOrientation(PercentageBar::Orientation orientation); + virtual PercentageBar::Orientation getOrientation() const; + virtual int getValue() const; + virtual void setValue(int value); + + enum Orientation + { + HORIZONTAL = 0, + VERTICAL + }; + }; }
--- a/engine/python/fife/extensions/pychan/widgets/__init__.py Mon Sep 27 14:22:07 2010 +0000 +++ b/engine/python/fife/extensions/pychan/widgets/__init__.py Mon Sep 27 15:38:09 2010 +0000 @@ -44,6 +44,7 @@ from dropdown import DropDown from scrollarea import ScrollArea from slider import Slider +from percentagebar import PercentageBar # Global Widget Class registry @@ -59,6 +60,7 @@ "Icon" : Icon, "Label" : Label, "ClickLabel" : ClickLabel, + "PercentageBar" : PercentageBar, # Button Widgets "Button" : Button,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine/python/fife/extensions/pychan/widgets/percentagebar.py Mon Sep 27 15:38:09 2010 +0000 @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- + +# #################################################################### +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net +# This file is part of FIFE. +# +# FIFE is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# #################################################################### + +from common import * +from widget import Widget + +class PercentageBar(Widget): + """ A percentage bar widget + + New Attributes + ============== + + - orientation: 1 = horizontal, 0=vertical + - value: int: default 0 + + """ + + HORIZONTAL = fife.PercentageBar.HORIZONTAL + VERTICAL = fife.PercentageBar.VERTICAL + + ATTRIBUTES = Widget.ATTRIBUTES + [IntAttr('orientation'), IntAttr('value')] + DEFAULT_HEXPAND = 1 + DEFAULT_VEXPAND = 0 + + def __init__(self, value=0, orientation=HORIZONTAL, min_size=(10,10),**kwargs): + self.real_widget = fife.PercentageBar() + self.orientation = orientation + self.setOrientation(self.orientation) + self.setValue(value) + super(PercentageBar, self).__init__(min_size=min_size,**kwargs) + + self.accepts_data = True + self._realSetData = self.setValue + self._realGetData = self.getValue + + def getValue(self): + """getValue(self) -> int""" + return self.real_widget.getValue() + + def setValue(self, value): + """setValue(self, int value)""" + if type(value) != int: + raise RuntimeError("PercentageBar only accepts integer values") + self.real_widget.setValue(value) + value = property(getValue, setValue) + + def setOrientation(self, orientation): + """setOrientation(self, Orientation orientation)""" + self.real_widget.setOrientation(orientation) + + def getOrientation(self): + """getOrientation(self) -> int""" + return self.real_widget.getOrientation() + orientation = property(getOrientation, setOrientation)