From 406646f5f0b583fe35a1ff448338ab8b294566cf Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 21 Aug 2018 18:59:07 +0100 Subject: [PATCH] [Add] More vscroll widget work. Still incomplete though. --- src/Makefile.am | 4 ++-- src/gui_adjustment.h | 16 ++++++++++++++++ src/gui_container.cpp | 9 +++++++++ src/gui_container.h | 2 ++ src/gui_fixed.cpp | 33 ++++++++++++++++++++++++--------- src/gui_fixed.h | 1 + src/gui_label.cpp | 20 +++++++++----------- src/gui_label.h | 1 + src/gui_screen.cpp | 13 +++++++++++++ src/gui_screen.h | 3 ++- src/gui_tooltip.cpp | 5 ++--- src/gui_widget.cpp | 8 ++++++++ src/gui_widget.h | 5 +++++ src/space_station_view.cpp | 14 +++++++++++++- 14 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 src/gui_adjustment.h diff --git a/src/Makefile.am b/src/Makefile.am index 53fd29d..433ceec 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,11 +10,11 @@ include_HEADERS = body.h frame.h generic_system_view.h glfreetype.h gui_button.h planet.h player.h dynamic_body.h sector.h sector_view.h ship_cpanel.h ship.h space.h star.h star_system.h system_info_view.h \ system_view.h vector3.h view.h world_view.h utils.h space_station.h space_station_view.h model_body.h gui_iselectable.h \ ship_type.h object.h info_view.h model_coll_mesh_data.h object_viewer_view.h fixed.h custom_starsystems.h gameconsts.h \ - aabb.h serializer.h sfx.h gui_vscroll_bar.h + aabb.h serializer.h sfx.h gui_vscroll_portal.h gui_vscroll_bar.h gui_adjustment.h libgui_a_SOURCES = gui_button.cpp gui.cpp gui_fixed.cpp gui_screen.cpp gui_label.cpp gui_tooltip.cpp gui_toggle_button.cpp gui_radio_button.cpp \ gui_radio_group.cpp gui_image_button.cpp gui_image.cpp gui_image_radio_button.cpp gui_multi_state_image_button.cpp gui_widget.cpp \ - gui_container.cpp gui_vscroll_bar.cpp + gui_container.cpp gui_vscroll_portal.cpp gui_vscroll_bar.cpp Lephisto3D_SOURCES = main.cpp glfreetype.cpp body.cpp space.cpp ship.cpp player.cpp dynamic_body.cpp planet.cpp \ star.cpp frame.cpp ship_cpanel.cpp sector_view.cpp mtrand.cpp world_view.cpp system_view.cpp \ diff --git a/src/gui_adjustment.h b/src/gui_adjustment.h new file mode 100644 index 0000000..f6ec33e --- /dev/null +++ b/src/gui_adjustment.h @@ -0,0 +1,16 @@ +#pragma once + +namespace Gui { + +class Adjustment { +public: + Adjustment(void): m_values(0) {} + float GetValue(void) { return m_value; } + void SetValue(float v) { m_value = (v>0?(v<1?v:1):0); } + +private: + float m_value; +}; + +} + diff --git a/src/gui_container.cpp b/src/gui_container.cpp index ad25ac1..e3bc856 100644 --- a/src/gui_container.cpp +++ b/src/gui_container.cpp @@ -103,6 +103,15 @@ void Container::AppendChild(Widget* child, float x, float y) { m_children.push_back(wp); } +void Container::RemoveChild(Widget* child) { + for(std::list::iterator i = m_children.begin(); i != m_children.end(); ++i) { + if((*i).w == child) { + m_children.erase(i); + return; + } + } +} + void Container::Draw(void) { for(std::list::iterator i = m_children.begin(); i != m_children.end(); ++i) { if(!(*i).w->IsVisible()) continue; diff --git a/src/gui_container.h b/src/gui_container.h index f347a8d..8d55051 100644 --- a/src/gui_container.h +++ b/src/gui_container.h @@ -15,12 +15,14 @@ namespace Gui { virtual void Draw(void); virtual void ShowAll(void); virtual void HideAll(void); + virtual void OnChildResizeRequest(Widget*) = 0; private: void _OnMouseLeave(void); bool HandleMouseEvent(MouseButtonEvent* e); protected: void PrependChild(Widget* w, float x, float y); void AppendChild(Widget* w, float x, float y); + void RemoveChild(Widget* w); struct widget_pos { Widget* w; diff --git a/src/gui_fixed.cpp b/src/gui_fixed.cpp index 5e9b0c0..e08cac0 100644 --- a/src/gui_fixed.cpp +++ b/src/gui_fixed.cpp @@ -20,29 +20,44 @@ Fixed::~Fixed(void) { } void Fixed::Draw(void) { + float size[2]; + GetSize(size); if(!m_transparent) { glBegin(GL_QUADS); glColor3f(m_bgcol[0], m_bgcol[1], m_bgcol[2]); - glVertex2f(0, m_h); - glVertex2f(m_w, m_h); - glVertex2f(m_w, 0); + glVertex2f(0, size[1]); + glVertex2f(size[0], size[1]); + glVertex2f(size[0], 0); glVertex2f(0, 0); glEnd(); } Container::Draw(); } +void Fixed::OnChildResizeRequest(Widget* child) { + for(std::list::iterator i = m_children.begin(); i != m_children.end(); ++i) { + if((*i).w == child) { + float rsize[2] = { m_w - (*i).pos[0], + m_h - (*i).pos[1] }; + child->GetSizeRequested(rsize); + if((*i).pos[0] + rsize[0] > m_w) rsize[0] = m_w - (*i).pos[0]; + if((*i).pos[1] + rsize[1] > m_h) rsize[1] = m_w - (*i).pos[1]; + child->SetSize(rsize[0], rsize[1]); + } + } +} + void Fixed::Add(Widget* child, float x, float y) { AppendChild(child, x, y); + float rsize[2] = { m_w - x, m_h - y }; + child->GetSizeRequested(rsize); + if(x+rsize[0] > m_w) rsize[0] = m_w-x; + if(y+rsize[1] > m_h) rsize[1] = m_h-y; + child->SetSize(rsize[0], rsize[1]); } void Fixed::Remove(Widget* child) { - for(std::list::iterator i = m_children.begin(); i != m_children.end(); ++i) { - if((*i).w == child) { - m_children.erase(i); - return; - } - } + Container::RemoveChild(child); } void Fixed::SetBgColor(float rgb[3]) { diff --git a/src/gui_fixed.h b/src/gui_fixed.h index b69b9c6..7246269 100644 --- a/src/gui_fixed.h +++ b/src/gui_fixed.h @@ -13,6 +13,7 @@ namespace Gui { virtual void Draw(void); virtual ~Fixed(void); virtual void GetSizeRequested(float size[2]); + virtual void OnChildResizeRequest(Widget*); void SetBgColor(float rgb[3]); void SetBgColor(float r, float g, float b); void SetTransparency(bool a) { m_transparent = a; } diff --git a/src/gui_label.cpp b/src/gui_label.cpp index 951a274..8c850e6 100644 --- a/src/gui_label.cpp +++ b/src/gui_label.cpp @@ -3,19 +3,21 @@ namespace Gui { Label::Label(const char* text) { + m_dlist = 0; SetText(text); m_color[0] = m_color[1] = m_color[2] = 1.0f; } Label::Label(std::string& text) { + m_dlist = 0; SetText(text); m_color[0] = m_color[1] = m_color[2] = 1.0f; } void Label::RecalcSize(void) { - float w, h; - Screen::MeasureString(m_text, w, h); - SetSize(w,h); + //float size[2]; + //Screen::MeasureLayout(m_text, FLT_MAX, size); + ResizeRequest(); } void Label::SetText(const char* text) { @@ -29,24 +31,20 @@ void Label::SetText(std::string& text) { } void Label::Draw(void) { -#if 0 float size[2]; GetSize(size); - glColor3f(1, 0, 0); + /*glColor3f(1, 0, 0); glBegin(GL_QUADS); glVertex2f(0, size[1]); glVertex2f(size[0]), size[1]); glVertex2f(size[0], 0); glVertex2f(0, 0); - glEnd(); -#endif /* 0 */ + glEnd();*/ glColor3fv(m_color); - Screen::RenderMarkup(m_text); - //Screen::LayoutString(m_text, 400); + Screen::LayoutString(m_text, size[0]); } void Label::GetSizeRequested(float size[2]) { - RecalcSize(); - GetSize(size); + Screen::MeasureLayout(m_text, size[0], size); } void Label::SetColor(float r, float g, float b) { diff --git a/src/gui_label.h b/src/gui_label.h index 452668f..12971c2 100644 --- a/src/gui_label.h +++ b/src/gui_label.h @@ -17,6 +17,7 @@ namespace Gui { void RecalcSize(void); std::string m_text; float m_color[3]; + GLuint m_dlist; }; } diff --git a/src/gui_screen.cpp b/src/gui_screen.cpp index 2baa6cf..1e6f4d8 100644 --- a/src/gui_screen.cpp +++ b/src/gui_screen.cpp @@ -140,6 +140,19 @@ void Screen::MeasureString(const std::string& s, float& w, float& h) { h *= fontScale; } +void Screen::MeasureLayout(const std::string& s, const float width, float outSize[2]) { + font->MeasureLayout(s.c_str(), width, outSize); + outSize[0] *= Screen::fontScale; + outSize[1] *= Screen::fontScale; +} + +void Screen::LayoutString(const std::string& s, const float width) { + glPushMatrix(); + glScalef(Screen::fontScale, Screen::fontScale, 1); + font->LayoutString(s.c_str(), width); + glPopMatrix(); +} + void Screen::RenderString(const std::string& s) { glPushMatrix(); glScalef(Screen::fontScale, Screen::fontScale, 1); diff --git a/src/gui_screen.h b/src/gui_screen.h index f0a2b24..59beed0 100644 --- a/src/gui_screen.h +++ b/src/gui_screen.h @@ -14,7 +14,8 @@ namespace Gui { static void OnMouseMotion(SDL_MouseMotionEvent* e); static void OnClick(SDL_MouseButtonEvent* e); static void OnKeyDown(const SDL_keysym* sym); - static void LayoutString(const std::string& s, float width); + static void LayoutString(const std::string& s, const float width); + static void MeasureLayout(const std::string& s, const float width, float outSize[2]); static void RenderString(const std::string& s); static void MeasureString(const std::string& s, float& w, float& h); static void RenderMarkup(const std::string& s); diff --git a/src/gui_tooltip.cpp b/src/gui_tooltip.cpp index 43f5dba..05ed4da 100644 --- a/src/gui_tooltip.cpp +++ b/src/gui_tooltip.cpp @@ -61,9 +61,8 @@ void ToolTip::Draw(void) { } void ToolTip::GetSizeRequested(float size[2]) { -#pragma message("Not setting size correctly.") - size[0] = 70; - size[1] = 10; + Screen::MeasureString(m_text, size[0], size[1]); + size[0] += 2 * TOOLTIP_PADDING; } } diff --git a/src/gui_widget.cpp b/src/gui_widget.cpp index 2674c79..7dd9ccd 100644 --- a/src/gui_widget.cpp +++ b/src/gui_widget.cpp @@ -109,6 +109,14 @@ void Widget::Hide(void) { } } +void Widget::ResizeRequest(void) { + if(m_parent) m_parent->OnChildrenResizeRequest(this); + else { + float size[2] = { FLT_MAX, FLT_MAX }; + GetSizeRequested(size); + SetSize(size[0], size[1]); + } + Widget::~Widget(void) { if(m_tooltipWidget) { Screen::RemoveBaseWidget(m_tooltipWidget); diff --git a/src/gui_widget.h b/src/gui_widget.h index 9dcbf63..66d2cf4 100644 --- a/src/gui_widget.h +++ b/src/gui_widget.h @@ -9,12 +9,17 @@ namespace Gui { Widget(void); virtual void Draw(void) = 0; virtual ~Widget(void); + /** + * Containers call this on children. input: size[] will contain max permissable size + * output: size[] will contain what space the widget desires. + */ virtual void GetSizeRequested(float size[2]) = 0; void GetPosition(float pos[2]) const { pos[0] = m_size.x; pos[1] = m_size.y; } void GetAbsolutePosition(float pos[2]); void SetPosition(float x, float y) { m_size.x = x; m_size.y = y; } void GetSize(float size[2]) { size[0] = m_size.w; size[1] = m_size.h; } void SetSize(float w, float h) { m_size.w = w; m_size.h = h; }; + void ResizeRequest(void); void SetShortcut(SDLKey key, SDLMod mod); void SetClipping(float width, float height); void EndClipping(void); diff --git a/src/space_station_view.cpp b/src/space_station_view.cpp index b4c8ffa..b82f3dd 100644 --- a/src/space_station_view.cpp +++ b/src/space_station_view.cpp @@ -31,6 +31,13 @@ private: StationFrontView::StationFrontView(SpaceStationView* parent): StationSubView(parent) { SetTransparency(false); + Gui::Fixed* fbox = new Gui::Fixed(720, 150); + Add(fbox, 40, 100); + + Gui::VScrollBar* scroll = new Gui::VScrollBar(); + Gui::VScrollPortal* box = new Gui::VScrollPortal(400, 150); + scroll->SetAdjustment(&box->vscollAdjust); + Gui::Label* l = new Gui::Label("Hello friend! Thankyou for docking with this space station! " "You may have noticed that the docking procedure was not entirely " "physically correct. this is a result of unimplemented physics in this " @@ -41,7 +48,12 @@ StationFrontView::StationFrontView(SpaceStationView* parent): StationSubView(par "can offer you this promotional message from one of the station's sponsors: \n" " ADOPT A CAT: THEY CHEW IMPORTANT CABLES!"); - Add(l, 40, 100); + + fbox->Add(box, 0, 0); + fbox->Add(scroll, 405, 0); + box->Add(l); + box->ShowAll(); + fbox->ShowAll(); Gui::SolidButton* b = new Gui::SolidButton(); b->onClick.connect(sigc::mem_fun(this, &StationFrontView::OnClickRequestLaunch));