From f330ae5ed3bd73c7b5b582cccde6cdd91d3c6e5b Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Wed, 24 Dec 2014 17:06:35 +0000
Subject: Added autopilot and control lock HUD buttons.

---
 src/client/hud.cc             |  6 +--
 src/client/hudenginestatus.cc | 85 +++++++++++++++++++++++++++++++++++++------
 src/client/hudenginestatus.h  | 18 +++++++++
 src/ui/palette.cc             | 19 ++++++++++
 src/ui/palette.h              | 29 +++++++++++----
 5 files changed, 134 insertions(+), 23 deletions(-)

diff --git a/src/client/hud.cc b/src/client/hud.cc
index 058b27a..1ae008c 100644
--- a/src/client/hud.cc
+++ b/src/client/hud.cc
@@ -57,11 +57,11 @@ void HUD::resize()
 	hud_playerstatus->set_size(w,ui::UI::elementsize.height() * 3.0f);
 	hud_playerstatus->set_location(padding, height() - hud_playerstatus->height() - padding);
 				       
-	hud_enginestatus->set_size(w,ui::UI::elementsize.height());
-	hud_enginestatus->set_location(hud_playerstatus->right() + padding, height() - hud_enginestatus->height() - padding);
+	hud_enginestatus->set_size(w,ui::UI::elementsize.height() * 3.0f);
+	hud_enginestatus->set_location(hud_playerstatus->right() + padding, hud_playerstatus->top());
 	
 	hud_targetstatus->set_size(w,ui::UI::elementsize.height() * 3.0f);
-	hud_targetstatus->set_location(hud_enginestatus->right() + padding, height() - hud_targetstatus->height() - padding);
+	hud_targetstatus->set_location(hud_enginestatus->right() + padding, hud_playerstatus->top());
 	
 }
 
diff --git a/src/client/hudenginestatus.cc b/src/client/hudenginestatus.cc
index 6b55344..6527b9a 100644
--- a/src/client/hudenginestatus.cc
+++ b/src/client/hudenginestatus.cc
@@ -4,11 +4,14 @@
    the terms and conditions of the GNU General Public License version 2
 */
 
+#include "client/input.h"
 #include "client/hudenginestatus.h"
 #include "core/core.h"
 #include "core/application.h"
 #include "ui/ui.h"
+#include "ui/iconbutton.h"
 #include "ui/paint.h"
+#include "render/render.h"
 #include "render/gl.h"
 
 namespace client
@@ -18,31 +21,60 @@ HUDEngineStatus::HUDEngineStatus(ui::Widget *parent) : ui::Widget(parent)
 {
 	set_border(false);
 	set_background(false);
+		
+	_button_auto_palette = new ui::Palette(*palette());
+	_button_auto = new ui::IconButton(this, "bitmaps/hud/button_auto", "freeflight");
+	_button_auto->set_palette(_button_auto_palette);
+	
+	_button_lock_palette = new ui::Palette(*palette());
+	_button_lock = new ui::IconButton(this, "bitmaps/hud/button_lock", "ui_control");
+	_button_lock->set_palette(_button_lock_palette);
 }
 
-void HUDEngineStatus::draw()
+HUDEngineStatus::~HUDEngineStatus()
 {
-	const float padding = font()->width() * 0.25f;
+	delete _button_auto_palette;
+	delete _button_lock_palette;
+}
+
+void HUDEngineStatus::resize()
+{
+	const float padding =  ui::root()->font_tiny()->width();
+	const float icon_size = 48.0f;
 	
-	math::Vector2f pos(global_location());
-	pos[0] += padding;
-	pos[1] += padding;
+	_button_auto->set_size(icon_size * 2.0f, icon_size);
+	_button_auto->set_location(padding, padding);
+	
+	_button_lock->set_size(icon_size * 2.0f, icon_size);
+	_button_lock->set_location(width() - padding - _button_lock->width(), padding);
+	
+}
+
+void HUDEngineStatus::draw()
+{
+	const float padding =  ui::root()->font_tiny()->width();
 	
 	math::Vector2f s(size());
 	s[0] -= 2.0f * padding;
-	s[1] -= 2.0f * padding;
+	s[1] = ui::root()->font_large()->height();
+	
+	math::Vector2f pos(global_location());
+	pos[0] += padding;
+	pos[1] += height() - s[1] - padding;
 	
 	// thrust
 	const float blink_t = core::application()->time() * 2.0f;
 	std::ostringstream thruststr;
-	switch(core::localcontrol()->state()) {
+	switch(core::localcontrol()->state())
+	{
 		case core::Entity::Normal:
 			thruststr << "^B" << roundf(core::localcontrol()->thrust() * 100.0f) << "%";
 			break;
 		case core::Entity::ImpulseInitiate:
 		case core::Entity::JumpInitiate:
 			
-			if ((blink_t - floorf(blink_t)) < 0.5f) {
+			if ((blink_t - floorf(blink_t)) < 0.5f)
+			{
 				thruststr << "^B--";
 			}
 			break;
@@ -56,9 +88,38 @@ void HUDEngineStatus::draw()
 	speedstr << "^B" << roundf(core::localcontrol()->speed() * 100.0f);
 	
 	ui::Paint::set_color(palette()->foreground());
-	ui::Paint::draw_label(pos, s, ui::root()->font_large(), thruststr.str(), ui::AlignLeft | ui::AlignVCenter);
-	ui::Paint::draw_label(pos, s, ui::root()->font_large(), speedstr.str(), ui::AlignRight | ui::AlignVCenter);
-		
+	ui::Paint::draw_label(pos, s, ui::root()->font_large(), thruststr.str(), ui::AlignLeft | ui::AlignTop);
+	ui::Paint::draw_label(pos, s, ui::root()->font_large(), speedstr.str(), ui::AlignRight | ui::AlignTop);
+	
+	// autopilot and control lock buttons
+
+	// see HUD::draw()
+	const bool control_lock =  ((core::localcontrol() && (input::mouse_control || input::joystick_control) && (render::Camera::mode() == render::Camera::Cockpit || render::Camera::mode() == render::Camera::Track)) ? false : true);
+
+	if (core::localplayer()->autopilot_target())
+	{
+		_button_auto->enable();
+		if (control_lock || ((blink_t - floorf(blink_t)) < 0.5f))
+		{
+			_button_auto_palette->set_foreground(palette()->mission());
+		} else
+		{
+			_button_auto_palette->set_foreground(palette()->foreground());
+		}
+	} else
+	{
+		_button_auto->disable();
+	}
+	
+	if (control_lock)
+	{
+		_button_lock_palette->set_foreground(palette()->pointer());
+	} else
+	{
+		// free controls
+		_button_lock_palette->set_foreground(palette()->foreground());
+	}
+	
 	// health bar size
 	const float hb_width = width() - 12 * ui::root()->font_large()->width() - 2.0f * padding;
 	const float hb_height = ui::root()->font_tiny()->width();
@@ -66,7 +127,7 @@ void HUDEngineStatus::draw()
 	
 	pos.assign(global_location());
 	pos[0] += (width() - hb_width) * 0.5f;
-	pos[1] += (height() - hb_height) * 0.5f;
+	pos[1] += height() - hb_height - padding;
 	
 	// health bar
 	gl::color(0, 1, 0, 1);
diff --git a/src/client/hudenginestatus.h b/src/client/hudenginestatus.h
index ff1c813..be1d322 100644
--- a/src/client/hudenginestatus.h
+++ b/src/client/hudenginestatus.h
@@ -9,6 +9,11 @@
 
 #include "ui/widget.h"
 
+namespace ui
+{
+class IconButton;
+}
+
 namespace client
 {
 
@@ -21,13 +26,26 @@ class HUDEngineStatus : public ui::Widget
 public:
 	/// create a new HUD widget
 	HUDEngineStatus(ui::Widget *parent = 0);
+	
+	/// destructor
+	virtual ~HUDEngineStatus();
 
 protected:
+	/// rearrange child widgets
+	virtual void resize();
+
 	/// draw hud elements
 	virtual void draw();
 
 	/// receive keyboard events
 	virtual bool on_keypress(const int key, const unsigned int modifier);
+	
+private:
+	ui::Palette	*_button_auto_palette;
+	ui::Palette	*_button_lock_palette;
+	
+	ui::IconButton	*_button_auto;
+	ui::IconButton	*_button_lock;
 };
 
 }
diff --git a/src/ui/palette.cc b/src/ui/palette.cc
index 6492dc3..9049b72 100644
--- a/src/ui/palette.cc
+++ b/src/ui/palette.cc
@@ -29,6 +29,25 @@ Palette::Palette() :
 
 }
 
+Palette::Palette(const Palette & other ) :
+		palette_foreground(other.foreground()),
+		palette_background(other.background()),
+		palette_border(other.border()),
+		palette_text(other.text()),
+		palette_highlight(other.highlight()),
+		palette_disabled(other.disabled()),
+		palette_pointer(other.pointer()),
+		palette_active(other.active()),
+		palette_debug(other.debug()),
+		palette_mission(other.mission()),
+		palette_bold(other.bold()),
+		palette_fancy(other.fancy()),
+		palette_warning(other.warning()),
+		palette_error(other.error())
+{
+
+}
+
 Palette::~Palette()
 {
 }
diff --git a/src/ui/palette.h b/src/ui/palette.h
index 422c0e7..ba5c456 100644
--- a/src/ui/palette.h
+++ b/src/ui/palette.h
@@ -12,22 +12,35 @@
 namespace ui
 {
 
-/// color palette used by the user interface
+/**
+ * @brief color palette used by the user interface
+ * */
 class Palette
 {
 
 public:
-	/// default constructor, creates a default palette
-	Palette();
-
-	/// default destructor
-	~Palette();
-
-	/// color index
+	/**
+	 * @brief color index
+	 * */
 	enum Color { Foreground = 0, Background = 1, Border = 2, Text = 3, Highlight = 4, Disabled = 5, Pointer = 6, Active = 7,
 		     Debug = 8, Mission = 9, Bold = 10, Fancy = 11, Warning = 12, Error = 13
 		   };
 
+	/**
+	 * @brief  default constructor, creates a default palette
+	 * */
+	Palette();
+	
+	/**
+	 * @brief copy construcotr
+	 * */
+	Palette(const Palette & other);
+
+	/** 
+	 * @brief default destructor
+	 * */
+	~Palette();
+	
 	/* ---- mutators ------------------------------------------- */
 
 	/// set foreground color
-- 
cgit v1.2.3