Skip to content

Commit

Permalink
NWN: Start reimplementing the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Jan 29, 2014
1 parent 04c4abb commit 5879ddc
Show file tree
Hide file tree
Showing 103 changed files with 12,807 additions and 60 deletions.
81 changes: 49 additions & 32 deletions src/engines/aurora/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include "events/events.h"

#include "graphics/graphics.h"
#include "graphics/renderable.h"
#include "graphics/guiman.h"
#include "graphics/cursorman.h"

#include "engines/aurora/gui.h"
#include "engines/aurora/widget.h"
Expand All @@ -54,22 +57,17 @@ GUI::~GUI() {
_widgets.clear();
}

void GUI::show() {
// Show all widgets
void GUI::setVisible(bool visible) {
LOCK_FRAME();

for (WidgetList::iterator w = _widgets.begin(); w != _widgets.end(); ++w) {
Widget &widget = **w;

if (!widget._owner)
widget.show();
widget.setVisible(visible);
}
}

void GUI::hide() {
// Hide all widgets
for (WidgetList::iterator widget = _widgets.begin(); widget != _widgets.end(); ++widget)
(*widget)->hide();
}

int GUI::run(int startCode) {
_startCode = startCode;
_returnCode = 0;
Expand Down Expand Up @@ -148,13 +146,17 @@ void GUI::addWidget(Widget *widget) {

_widgets.push_back(widget);
_widgetMap[widget->getTag()] = widget;

const std::vector<Common::UString> ids = widget->getIDs();
for (std::vector<Common::UString>::const_iterator i = ids.begin(); i != ids.end(); ++i)
_widgetIDMap[*i] = widget;
}

void GUI::removeWidget(Widget *widget) {
if (!widget)
return;

widget->hide();
widget->setVisible(false);

for (WidgetList::iterator i = _widgets.begin(); i != _widgets.end(); ++i) {
if (*i == widget) {
Expand All @@ -167,6 +169,13 @@ void GUI::removeWidget(Widget *widget) {
if (w != _widgetMap.end())
_widgetMap.erase(w);

const std::vector<Common::UString> ids = widget->getIDs();
for (std::vector<Common::UString>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
WidgetIDMap::iterator wI = _widgetIDMap.find(*i);
if (wI != _widgetIDMap.end())
_widgetIDMap.erase(wI);
}

if (widget->_parent)
widget->_parent->removeChild(*widget);
if (widget->_owner)
Expand Down Expand Up @@ -218,29 +227,39 @@ void GUI::declareGroup(const std::list<Widget *> &group) {
}

int GUI::sub(GUI &gui, int startCode, bool showSelf) {
GfxMan.lockFrame();

removeFocus();

// Show the sub GUI
if (startCode == 0)
gui.show();
hide();
gui.setVisible(true);
setVisible(false);

GfxMan.unlockFrame();

// Run the sub GUI
int code = gui.run(startCode);

GfxMan.lockFrame();

// Hide the sub GUI
if (showSelf)
show();
gui.hide();
setVisible(true);
gui.setVisible(false);

// Update the mouse position
removeFocus();
updateMouse();

GfxMan.unlockFrame();

return code;
}

void GUI::setPosition(float x, float y, float z) {
LOCK_FRAME();

for (WidgetList::iterator w = _widgets.begin(); w != _widgets.end(); ++w) {
Widget &widget = **w;

Expand Down Expand Up @@ -274,7 +293,8 @@ void GUI::removeFocus() {

void GUI::updateMouse() {
// Fabricate a mouse move event at the current position
int x = 0, y = 0, state = 0;
int x, y, state;
state = CursorMan.getPosition(x, y);

Events::Event event;
event.motion.state = state;
Expand All @@ -286,7 +306,16 @@ void GUI::updateMouse() {
}

Widget *GUI::getWidgetAt(float x, float y) {
return 0;
float distance;
Graphics::Renderable *renderable = GUIMan.getRenderableAt(x, y, distance);
if (!renderable)
return 0;

WidgetIDMap::iterator w = _widgetIDMap.find(renderable->getID());
if (w == _widgetIDMap.end())
return 0;

return w->second;
}

void GUI::changedWidget(Widget *widget) {
Expand Down Expand Up @@ -386,33 +415,21 @@ void GUI::mouseWheel(const Events::Event &event) {
mouseWheel(_currentWidget, event);
}

float GUI::toGUIX(int x) {
float sW = GfxMan.getScreenWidth();

return (x - (sW / 2.0));
}

float GUI::toGUIY(int y) {
float sH = GfxMan.getScreenHeight();

return ((sH - y) - (sH / 2.0));
}

void GUI::mouseMove(Widget *widget, const Events::Event &event) {
if (widget)
widget->mouseMove(event.motion.state, toGUIX(event.motion.x), toGUIY(event.motion.y));
widget->mouseMove(event.motion.state, event.motion.x, event.motion.y);
}

void GUI::mouseDown(Widget *widget, const Events::Event &event) {
if (widget)
widget->mouseDown(event.button.button, toGUIX(event.button.x), toGUIY(event.button.y));
widget->mouseDown(event.button.button, event.button.x, event.button.y);
}

void GUI::mouseUp(Widget *widget, const Events::Event &event) {
if (widget) {
uint8 button = event.button.button;
float x = toGUIX(event.button.x);
float y = toGUIY(event.button.y);
float x = event.button.x;
float y = event.button.y;

widget->mouseUp(button, x, y);

Expand Down
13 changes: 6 additions & 7 deletions src/engines/aurora/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class GUI {
GUI();
virtual ~GUI();

virtual void show(); ///< Show the GUI.
virtual void hide(); ///< Hide the GUI.
/** Show/Hide the GUI. */
virtual void setVisible(bool visible);

int run(int startCode = 0); ///< Run the GUI.

Expand Down Expand Up @@ -103,9 +103,11 @@ class GUI {
private:
typedef std::list<Widget *> WidgetList;
typedef std::map<Common::UString, Widget *> WidgetMap;
typedef std::map<Common::UString, Widget *> WidgetIDMap;

WidgetList _widgets; ///< All widgets in the GUI.
WidgetMap _widgetMap; ///< All widgets in the GUI, index by their tag.
WidgetList _widgets; ///< All widgets in the GUI.
WidgetMap _widgetMap; ///< All widgets in the GUI, index by their tag.
WidgetIDMap _widgetIDMap; ///< Widgets mapped from ID to tag.

float _x; ///< The GUI X position.
float _y; ///< The GUI Y position.
Expand All @@ -124,9 +126,6 @@ class GUI {
void mouseUp(const Events::Event &event); ///< Mouse up event triggered.
void mouseWheel(const Events::Event &event); ///< Mouse wheel event triggered.

float toGUIX(int x); // Convert an event X coordinate to a GUI X coordinate
float toGUIY(int y); // Convert an event Y coordinate to a GUI Y coordinate

/** Send a mouse move event to the widget. */
void mouseMove(Widget *widget, const Events::Event &event);
/** Send a mouse down event to the widget. */
Expand Down
41 changes: 22 additions & 19 deletions src/engines/aurora/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* A widget in a GUI.
*/

#include "graphics/graphics.h"

#include "engines/aurora/widget.h"
#include "engines/aurora/gui.h"

Expand All @@ -43,6 +45,10 @@ Widget::Widget(GUI &gui, const Common::UString &tag) : _gui(&gui), _tag(tag),
Widget::~Widget() {
}

const std::vector<Common::UString> &Widget::getIDs() const {
return _ids;
}

const Common::UString &Widget::getTag() const {
return _tag;
}
Expand All @@ -69,9 +75,9 @@ bool Widget::isInvisible() const {
return _invisible;
}

void Widget::show() {
if (_visible)
// Already shown, nothing to do

void Widget::setVisible(bool visible) {
if (_visible == visible)
return;

// Reset the double-click info
Expand All @@ -80,24 +86,13 @@ void Widget::show() {
_lastClickX = 0.0;
_lastClickY = 0.0;

if (!_invisible)
_visible = true;
LOCK_FRAME();

// Show children
// Change the children's visibility
for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
(*it)->show();
}

void Widget::hide() {
if (!_visible)
// Already hidden, nothing to do
return;
(*it)->setVisible(visible);

_visible = false;

// Hide children
for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
(*it)->hide();
_visible = _invisible ? false : visible;
}

Widget *Widget::getParent() {
Expand All @@ -109,6 +104,8 @@ const Widget *Widget::getParent() const {
}

void Widget::setPosition(float x, float y, float z) {
LOCK_FRAME();

for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it) {
float sX, sY, sZ;
(*it)->getPosition(sX, sY, sZ);
Expand Down Expand Up @@ -150,6 +147,8 @@ void Widget::setDisabled(bool disabled) {

_disabled = disabled;

LOCK_FRAME();

// Disable/Enable children
for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
(*it)->setDisabled(disabled);
Expand All @@ -164,6 +163,8 @@ void Widget::setInvisible(bool invisible) {

_invisible = invisible;

LOCK_FRAME();

// Invisible the children
for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
(*it)->setInvisible(invisible);
Expand Down Expand Up @@ -247,7 +248,7 @@ void Widget::removeGroupMember(Widget &widget) {
}

void Widget::remove() {
hide();
setVisible(false);

_gui->removeWidget(this);
}
Expand All @@ -263,6 +264,8 @@ void Widget::setActive(bool active) {

_active = active;

LOCK_FRAME();

// Signal our group members that we're active now
if (_active)
for (std::list<Widget *>::iterator it = _groupMembers.begin(); it != _groupMembers.end(); ++it)
Expand Down
9 changes: 7 additions & 2 deletions src/engines/aurora/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Widget {
Widget(GUI &gui, const Common::UString &tag);
virtual ~Widget();

/** Return the IDs of the widget's renderable(s). */
const std::vector<Common::UString> &getIDs() const;

const Common::UString &getTag() const; ///< Get the widget's tag.

/** Set the widget's tag. */
Expand All @@ -55,8 +58,8 @@ class Widget {
bool isDisabled () const; ///< Is the widget disabled?
bool isInvisible() const; ///< Is the widget invisible (never visible)?

virtual void show(); ///< Show the widget.
virtual void hide(); ///< Hide the widget.
/** Show/Hide the widget. */
virtual void setVisible(bool visible);

Widget *getParent();
const Widget *getParent() const;
Expand Down Expand Up @@ -107,6 +110,8 @@ class Widget {
protected:
GUI *_gui; ///< The GUI the widget belongs to.

std::vector<Common::UString> _ids; ///< IDs of the widgets's renderable(s).

Common::UString _tag; ///< The widget's tag.

Widget *_parent; ///< The widget's parent, if any.
Expand Down
34 changes: 34 additions & 0 deletions src/engines/nwn/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ noinst_HEADERS = \
door.h \
placeable.h \
creature.h \
gui/widgets/nwnwidget.h \
gui/widgets/modelwidget.h \
gui/widgets/frame.h \
gui/widgets/panel.h \
gui/widgets/button.h \
gui/widgets/close.h \
gui/widgets/textwidget.h \
gui/widgets/label.h \
gui/gui.h \
gui/main/main.h \
gui/main/moviesbase.h \
gui/main/moviescamp.h \
gui/main/new.h \
gui/main/newcamp.h \
gui/main/newxp1.h \
gui/main/newxp2.h \
gui/main/chartype.h \
$(EMPTY)

libnwn_la_SOURCES = \
Expand All @@ -35,4 +52,21 @@ libnwn_la_SOURCES = \
door.cpp \
placeable.cpp \
creature.cpp \
gui/widgets/nwnwidget.cpp \
gui/widgets/modelwidget.cpp \
gui/widgets/frame.cpp \
gui/widgets/panel.cpp \
gui/widgets/button.cpp \
gui/widgets/close.cpp \
gui/widgets/textwidget.cpp \
gui/widgets/label.cpp \
gui/gui.cpp \
gui/main/main.cpp \
gui/main/moviesbase.cpp \
gui/main/moviescamp.cpp \
gui/main/new.cpp \
gui/main/newcamp.cpp \
gui/main/newxp1.cpp \
gui/main/newxp2.cpp \
gui/main/chartype.cpp \
$(EMPTY)

0 comments on commit 5879ddc

Please sign in to comment.