Permalink
Browse files

Merge remote-tracking branch 'johnbartholomew/view-cleanup'

  • Loading branch information...
2 parents 9bdd062 + beb8b52 commit 253837d3541a301b2ef6478c629945f7c9edc418 @robn robn committed Sep 23, 2012
View
@@ -33,6 +33,7 @@ Alpha 27
* Copyright and license is now made explicit at the top of all code files (#1515)
* Max terrain height for heightmapped planets is computed more accurately (#1504)
* Code to detect supported video modes has moved to Graphics (#1535, #1385)
+ * Cleaned up UI view code for consistency and stability (#1528)
Alpha 26
View
@@ -19,6 +19,7 @@ class GalacticView: public View {
virtual void Draw3D();
virtual void Save(Serializer::Writer &wr);
virtual void Load(Serializer::Reader &rd);
+protected:
virtual void OnSwitchTo() {}
private:
void OnClickGalacticView();
View
@@ -63,11 +63,12 @@ class GameMenuView: public View {
virtual ~GameMenuView();
virtual void Update() {}
virtual void Draw3D() {}
- virtual void OnSwitchTo();
virtual void ShowAll();
virtual void HideAll();
void OpenLoadDialog();
void OpenSaveDialog();
+protected:
+ virtual void OnSwitchTo();
private:
void OnChangeKeyBinding(const KeyBindings::KeyBinding &kb, const char *fnName);
void OnChangeAxisBinding(const KeyBindings::AxisBinding &ab, const char *function);
View
@@ -18,10 +18,11 @@ class InfoView: public View {
void UpdateInfo();
virtual void Update();
virtual void Draw3D();
- virtual void OnSwitchTo();
void NextPage();
void ShowSpinner() { m_showSpinner = true; }
void HideSpinner() { m_showSpinner = false; }
+protected:
+ virtual void OnSwitchTo();
private:
std::list<InfoViewPage*> m_pages;
Gui::Tabbed *m_tabs;
View
@@ -17,6 +17,7 @@ class ObjectViewerView: public View {
ObjectViewerView();
virtual void Update();
virtual void Draw3D();
+protected:
virtual void OnSwitchTo();
private:
float viewingDist;
View
@@ -474,14 +474,9 @@ void Pi::BoinkNoise()
void Pi::SetView(View *v)
{
- if (cpan)
- cpan->ClearOverlay();
- if (currentView) currentView->HideAll();
+ if (currentView) currentView->Detach();
currentView = v;
- if (currentView) {
- currentView->OnSwitchTo();
- currentView->ShowAll();
- }
+ if (currentView) currentView->Attach();
}
void Pi::OnChangeDetailLevel()
View
@@ -35,10 +35,11 @@ class SectorView: public View {
void GotoSelectedSystem() { GotoSystem(m_selected); }
void GotoHyperspaceTarget() { GotoSystem(m_hyperspaceTarget); }
virtual void Save(Serializer::Writer &wr);
- virtual void OnSwitchTo();
sigc::signal<void> onHyperspaceTargetChanged;
+protected:
+ virtual void OnSwitchTo();
private:
void InitDefaults();
void InitObject();
View
@@ -17,8 +17,9 @@ class SpaceStationView: public View {
virtual ~SpaceStationView();
virtual void Update();
virtual void Draw3D() {}
- virtual void OnSwitchTo();
+protected:
+ virtual void OnSwitchTo();
private:
void RefreshForForm(Form *);
View
@@ -261,7 +261,7 @@ void SystemInfoView::SystemChanged(const SystemPath &path)
Gui::Label *l = (new Gui::Label(_info))->Color(1.0f,1.0f,0.0f);
m_sbodyInfoTab->Add(l, 35, 300);
- Fixed::ShowAll(); // bypass View::ShowAll which does extra stuff that we don't want
+ ShowAll();
return;
}
@@ -397,7 +397,7 @@ void SystemInfoView::SystemChanged(const SystemPath &path)
UpdateIconSelections();
- Fixed::ShowAll(); // bypass View::ShowAll which does extra stuff that we don't want
+ ShowAll();
}
void SystemInfoView::Draw3D()
View
@@ -7,7 +7,6 @@
#include "libs.h"
#include "gui/Gui.h"
#include "View.h"
-#include "View.h"
#include <vector>
class StarSystem;
@@ -19,8 +18,9 @@ class SystemInfoView: public View {
SystemInfoView();
virtual void Update();
virtual void Draw3D();
- virtual void OnSwitchTo();
void NextPage();
+protected:
+ virtual void OnSwitchTo();
private:
class BodyIcon : public Gui::ImageRadioButton {
public:
View
@@ -18,6 +18,7 @@ class SystemView: public View {
virtual ~SystemView();
virtual void Update();
virtual void Draw3D();
+protected:
virtual void OnSwitchTo() {}
private:
static const double PICK_OBJECT_RECT_SIZE;
View
@@ -27,32 +27,34 @@ View::~View() {
delete m_rightRegion1;
}
-void View::ShowAll() {
+void View::Attach() {
+ OnSwitchTo();
+
+ const float w = float(Gui::Screen::GetWidth());
+ const float h = float(Gui::Screen::GetHeight());
+
Gui::Screen::AddBaseWidget(this, 0, 0);
- if (Pi::game) {
- Gui::Screen::AddBaseWidget(Pi::cpan, 0, Gui::Screen::GetHeight()-80);
- Gui::Screen::AddBaseWidget(m_rightButtonBar, Gui::Screen::GetWidth()-128, Gui::Screen::GetHeight()-26);
- Gui::Screen::AddBaseWidget(m_rightRegion2, Gui::Screen::GetWidth()-127, Gui::Screen::GetHeight()-45);
- Gui::Screen::AddBaseWidget(m_rightRegion1, Gui::Screen::GetWidth()-123, Gui::Screen::GetHeight()-62);
+ if (Pi::cpan) {
+ Gui::Screen::AddBaseWidget(Pi::cpan, 0, h-80);
+ Gui::Screen::AddBaseWidget(m_rightButtonBar, w-128, h-26);
+ Gui::Screen::AddBaseWidget(m_rightRegion2, w-127, h-45);
+ Gui::Screen::AddBaseWidget(m_rightRegion1, w-123, h-62);
m_rightButtonBar->ShowAll();
m_rightRegion2->ShowAll();
m_rightRegion1->ShowAll();
}
- Gui::Fixed::ShowAll();
+ ShowAll();
}
-void View::HideAll() {
- m_rightButtonBar->HideAll();
- m_rightRegion2->HideAll();
- m_rightRegion1->HideAll();
- Gui::Fixed::HideAll();
-
+void View::Detach() {
Gui::Screen::RemoveBaseWidget(m_rightButtonBar);
Gui::Screen::RemoveBaseWidget(m_rightRegion2);
Gui::Screen::RemoveBaseWidget(m_rightRegion1);
Gui::Screen::RemoveBaseWidget(Pi::cpan);
Gui::Screen::RemoveBaseWidget(this);
+ if (Pi::cpan)
+ Pi::cpan->ClearOverlay();
}
View
@@ -21,19 +21,21 @@ class View: public Gui::Fixed {
public:
View();
virtual ~View();
- virtual void ShowAll();
- virtual void HideAll();
// called before Gui::Draw will call widget ::Draw methods.
virtual void Draw3D() = 0;
// for checking key states, mouse crud
virtual void Update() = 0;
virtual void Save(Serializer::Writer &wr) {}
virtual void Load(Serializer::Reader &rd) {}
- virtual void OnSwitchTo() = 0;
+
+ void Attach();
+ void Detach();
void SetRenderer(Graphics::Renderer *r) { m_renderer = r; }
protected:
+ virtual void OnSwitchTo() = 0;
+
// each view can put some buttons in the bottom right of the cpanel
Gui::Fixed *m_rightButtonBar;
Gui::Fixed *m_rightRegion1;
View
@@ -30,7 +30,6 @@ class WorldView: public View {
virtual void Update();
virtual void Draw3D();
virtual void Draw();
- virtual void OnSwitchTo();
static const double PICK_OBJECT_RECT_SIZE;
void DrawBgStars();
virtual void Save(Serializer::Writer &wr);
@@ -58,6 +57,8 @@ class WorldView: public View {
sigc::signal<void> onChangeCamType;
+protected:
+ virtual void OnSwitchTo();
private:
void InitObject();
View
@@ -120,6 +120,9 @@ void Container::RemoveAllChildren()
void Container::PrependChild(Widget *child, float x, float y)
{
+ assert(child->GetParent() == 0);
+ assert(FindChild(child) == m_children.end());
+
widget_pos wp;
wp.w = child;
wp.pos[0] = x; wp.pos[1] = y;
@@ -130,6 +133,9 @@ void Container::PrependChild(Widget *child, float x, float y)
void Container::AppendChild(Widget *child, float x, float y)
{
+ assert(child->GetParent() == 0);
+ assert(FindChild(child) == m_children.end());
+
widget_pos wp;
wp.w = child;
wp.pos[0] = x; wp.pos[1] = y;
@@ -140,26 +146,35 @@ void Container::AppendChild(Widget *child, float x, float y)
void Container::MoveChild(Widget *child, float x, float y)
{
- for (std::list<widget_pos>::iterator i = m_children.begin(); i != m_children.end(); ++i) {
- if ((*i).w == child) {
- (*i).pos[0] = x;
- (*i).pos[1] = y;
- return;
- }
+ std::list<widget_pos>::iterator it = FindChild(child);
+ if (it != m_children.end()) {
+ it->pos[0] = x;
+ it->pos[1] = y;
}
}
void Container::RemoveChild(Widget *child)
{
- for (std::list<widget_pos>::iterator i = m_children.begin(); i != m_children.end(); ++i) {
- if ((*i).w == child) {
- child->SetParent(0);
- m_children.erase(i);
- return;
- }
+ std::list<widget_pos>::iterator it = FindChild(child);
+ if (it != m_children.end()) {
+ it->w->SetParent(0);
+ m_children.erase(it);
}
}
+Container::WidgetList::const_iterator Container::FindChild(const Widget *w) const
+{
+ for (std::list<widget_pos>::const_iterator i = m_children.begin(); i != m_children.end(); ++i)
+ if (i->w == w) return i;
+ return m_children.end();
+}
+
+Container::WidgetList::iterator Container::FindChild(const Widget *w)
+{
+ for (std::list<widget_pos>::iterator i = m_children.begin(); i != m_children.end(); ++i)
+ if (i->w == w) return i;
+ return m_children.end();
+}
void Container::Draw()
{
@@ -238,14 +253,10 @@ void Container::HideChildren()
void Container::GetChildPosition(const Widget *child, float outPos[2]) const
{
- for (std::list<widget_pos>::const_iterator i = m_children.begin(); i != m_children.end(); ++i) {
- if ((*i).w == child) {
- outPos[0] = (*i).pos[0];
- outPos[1] = (*i).pos[1];
- return;
- }
- }
- assert(0);
+ WidgetList::const_iterator it = FindChild(child);
+ assert(it != m_children.end());
+ outPos[0] = it->pos[0];
+ outPos[1] = it->pos[1];
}
void Container::Show()
View
@@ -43,16 +43,20 @@ namespace Gui {
Color m_bgcol;
bool m_transparent;
protected:
- void PrependChild(Widget *w, float x, float y);
- void AppendChild(Widget *w, float x, float y);
- void MoveChild(Widget *w, float x, float y);
-
struct widget_pos {
Widget *w;
float pos[2];
Uint32 flags;
};
- std::list<widget_pos> m_children;
+ typedef std::list<widget_pos> WidgetList;
+
+ void PrependChild(Widget *w, float x, float y);
+ void AppendChild(Widget *w, float x, float y);
+ void MoveChild(Widget *w, float x, float y);
+ WidgetList::const_iterator FindChild(const Widget *w) const;
+ WidgetList::iterator FindChild(const Widget *w);
+
+ WidgetList m_children;
};
}
View
@@ -61,7 +61,6 @@ void Fixed::GetSizeRequested(float size[2])
Fixed::~Fixed()
{
- Screen::RemoveBaseWidget(this);
}
void Fixed::UpdateAllChildSizes()

0 comments on commit 253837d

Please sign in to comment.