Panagiotis Christopoulos Charitos пре 10 година
родитељ
комит
47f9660f16

+ 3 - 5
include/anki/input/Input.h

@@ -3,8 +3,7 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
-#ifndef ANKI_INPUT_INPUT_H
-#define ANKI_INPUT_INPUT_H
+#pragma once
 
 #include "anki/Math.h"
 #include "anki/util/Singleton.h"
@@ -74,8 +73,8 @@ public:
 	/// Populate the key and button with the new state
 	ANKI_USE_RESULT Error handleEvents();
 
-	/// Move the mouse cursor to a position inside the window. Useful for 
-	/// locking the cursor into a fixed location (eg in the center of the 
+	/// Move the mouse cursor to a position inside the window. Useful for
+	/// locking the cursor into a fixed location (eg in the center of the
 	/// screen)
 	void moveCursor(const Vec2& posNdc);
 
@@ -126,4 +125,3 @@ private:
 
 } // end namespace anki
 
-#endif

+ 10 - 5
include/anki/input/KeyCode.h

@@ -3,12 +3,11 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
-#ifndef ANKI_INPUT_KEY_CODES_H
-#define ANKI_INPUT_KEY_CODES_H
+#pragma once
 
 namespace anki {
 
-/// Keyboard scancodes taken from SDL 
+/// Keyboard scancodes taken from SDL
 enum class KeyCode
 {
 	UNKNOWN = 0,
@@ -259,11 +258,17 @@ enum class KeyCode
 	KBDILLUMDOWN,
 	KBDILLUMUP,
 	EJECT,
-	SLEEP,	
+	SLEEP,
 
 	COUNT
 };
 
+enum class MouseButton
+{
+	LEFT,
+	MIDDLE,
+	RIGHT
+};
+
 } // end namespace anki
 
-#endif

+ 42 - 0
include/anki/ui/Canvas.h

@@ -0,0 +1,42 @@
+// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include "anki/ui/Common.h"
+#include "anki/util/Allocator.h"
+
+namespace anki {
+
+/// @addtogroup ui
+/// @{
+
+/// UI canvas.
+class Canvas
+{
+public:
+	Canvas(UiAllocator alloc, Painter* painter, ImageManager* imageMngr)
+		: m_alloc(alloc)
+		, m_painter(painter)
+		, m_img(imageMngr)
+	{}
+
+	UiAllocator& getAllocator()
+	{
+		return m_alloc;
+	}
+
+	const UiAllocator& getAllocator() const
+	{
+		return m_alloc;
+	}
+
+private:
+	UiAllocator m_alloc;
+	WeakPtr<Painter> m_painter;
+	WeakPtr<ImageManager> m_img;
+};
+/// @}
+

+ 52 - 0
include/anki/ui/Common.h

@@ -0,0 +1,52 @@
+// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include "anki/Math.h"
+#include "anki/util/Allocator.h"
+#include "anki/util/Ptr.h"
+
+namespace anki {
+
+// Forward
+class Painter;
+class Image;
+
+/// @addtogroup ui
+/// @{
+
+// Pointers
+using ImagePtr = IntrusivePtr<Image>;
+
+using UiAllocator = GenericMemoryPoolAllocator<U8>;
+
+/// Color.
+using Color = Vec4;
+
+/// Rectangle.
+class Rect
+{
+public:
+	Vec2 m_min = {0.0};
+	Vec2 m_max = {getEpsilon<F32>()};
+
+	Rect() = default;
+
+	Rect(Vec2 min, Vec2 max)
+		: m_min(min)
+		, m_max(max)
+	{
+		ANKI_ASSERT(m_min < m_max);
+	}
+
+	Rect(const Rect&) = default;
+
+	Rect& operator=(const Rect&) = default;
+};
+/// @}
+
+} // end namespace anki
+

+ 24 - 0
include/anki/ui/Font.h

@@ -0,0 +1,24 @@
+// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include "anki/ui/UiObject.h"
+
+namespace anki {
+
+/// @addtogroup ui
+/// @{
+
+class Font
+/// @}
+
+} // end namespace anki
+

+ 0 - 43
include/anki/ui/UiCommon.h

@@ -1,43 +0,0 @@
-// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#ifndef ANKI_UI_UI_COMMON_H
-#define ANKI_UI_UI_COMMON_H
-
-#include "anki/Math.h"
-#include "anki/util/Allocator.h"
-
-namespace anki {
-
-/// @addtogroup ui
-/// @{
-
-/// Color
-typedef Vec4 UiColor;
-
-/// 2D point
-typedef Vec2 UiPoint;
-
-/// 2D position
-typedef Vec2 UiPosition;
-
-/// Rectangle
-struct UiRect
-{
-	UiPosition min;
-	UiPosition max;
-
-	UiRect(UiPosition min_, UiPosition max_)
-		: min(min_), max(max_)
-	{
-		ANKI_ASSERT(min < max);
-	}
-};
-
-/// @}
-
-} // end namespace anki
-
-#endif

+ 0 - 97
include/anki/ui/UiFont.h

@@ -1,97 +0,0 @@
-// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#ifndef ANKI_UI_UI_FONT_H
-#define ANKI_UI_UI_FONT_H
-
-#include <boost/ptr_container/ptr_vector.hpp>
-#include "anki/Math.h"
-
-namespace anki {
-
-class Texture;
-
-/// The font is device agnostic, all sizes are in actual pixels
-class UiFont
-{
-public:
-	/// Constructor @see create
-	UiFont(const char* fontFilename, uint nominalWidth, uint nominalHeight)
-	{
-		create(fontFilename, nominalWidth, nominalHeight);
-	}
-
-	/// @name Accessors
-	/// @{
-	const Mat3& getGlyphTextureMatrix(char c) const
-	{
-		return glyphs[c - ' '].textureMat;
-	}
-
-	uint32_t getGlyphWidth(char c) const
-	{
-		return glyphs[c - ' '].width;
-	}
-
-	uint32_t getGlyphHeight(char c) const
-	{
-		return glyphs[c - ' '].height;
-	}
-
-	int getGlyphAdvance(char c) const
-	{
-		return glyphs[c - ' '].horizAdvance;
-	}
-
-	int getGlyphBearingX(char c) const
-	{
-		return glyphs[c - ' '].horizBearingX;
-	}
-
-	int getGlyphBearingY(char c) const
-	{
-		return glyphs[c - ' '].horizBearingY;
-	}
-
-	const Texture& getMap() const
-	{
-		return *map;
-	}
-
-	uint32_t getLineHeight() const
-	{
-		return lineHeight;
-	}
-	/// @}
-
-private:
-	struct Glyph
-	{
-		/// Transforms the default texture coordinates to the glyph's. The
-		/// default are (1, 1), (0, 1), (0, 0), (0, 1)
-		Mat3 textureMat;
-		uint32_t width;
-		uint32_t height;
-		int horizBearingX;
-		int horizBearingY;
-		int horizAdvance;
-	};
-
-	/// The texture map that contains all the glyphs
-	std::unique_ptr<Texture> map;
-	/// A set of glyphs from ' ' to ' ' + 128
-	boost::ptr_vector<Glyph> glyphs;
-	uint32_t lineHeight;
-
-	/// @param[in] fontFilename The filename of the font to load
-	/// @param[in] nominalWidth The nominal glyph width in pixels
-	/// @param[in] nominalHeight The nominal glyph height in pixels
-	void create(const char* fontFilename, uint32_t nominalWidth,
-		uint32_t nominalHeight);
-};
-
-} // end namespace anki
-
-#endif

+ 0 - 112
include/anki/ui/UiFtFontLoader.h

@@ -1,112 +0,0 @@
-// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#ifndef ANKI_UI_UI_FrustumType::FONT_LOADER_H
-#define ANKI_UI_UI_FrustumType::FONT_LOADER_H
-
-#include "anki/Math.h"
-#include "anki/util/Vector.h"
-#include <boost/range/iterator_range.hpp>
-#include <ft2build.h>
-#include FrustumType::FREETYPE_H
-#include FrustumType::GLYPH_H
-
-namespace anki {
-
-/// A helper class that uses libfreetype to load glyphs from a font file and
-/// gather the metrics for each glyph
-class UiFtFontLoader
-{
-public:
-	/// Contains info about the glyphs
-	class Glyph
-	{
-		friend class UiFtFontLoader;
-
-	public:
-		FrustumType::Glyph_Metrics getMetrics() const
-		{
-			return metrics;
-		}
-
-	private:
-		FrustumType::Glyph glyph;
-		FrustumType::Glyph_Metrics metrics;
-	};
-
-	enum
-	{
-		GLYPHS_NUM = 128,
-		GLYPH_COLUMNS = 16,
-		GLYPH_ROWS = 8
-	};
-
-	/// One and only constructor
-	UiFtFontLoader(const char* filename, const FrustumType::Vector& fontSize)
-	{
-		createImage(filename, fontSize);
-	}
-
-	/// @name Accessors
-	/// @{
-	const uint8_t* getImage() const
-	{
-		return &img[0];
-	}
-
-	const FrustumType::Vector& getImageSize() const
-	{
-		return imgSize;
-	}
-
-	boost::iterator_range<Vector<Glyph>::const_iterator>
-		getGlyphs() const
-	{
-		return boost::iterator_range<Vector<Glyph>::const_iterator>(
-			glyphs.begin(), glyphs.end());
-	}
-
-	uint getLineHeight() const
-	{
-		return lineHeight;
-	}
-	/// @}
-
-	/// Save the image (img) to TGA. Its for debugging purposes
-	void saveImage(const char* filename) const;
-
-	static FrustumType::Int toPixels(FrustumType::Int a)
-	{
-		return a >> 6;
-	}
-
-private:
-	/// @name Data
-	/// @{
-	FrustumType::Library library;
-	FrustumType::Face face;
-	Vector<Glyph> glyphs;
-	Vector<uint8_t> img;
-	FrustumType::Vector imgSize;
-	uint32_t lineHeight; ///< Calculated as the max height among all glyphs
-	/// @}
-
-	/// Reads the face and extracts the glyphs
-	void getAllGlyphs();
-
-	/// Copy one bitmap to img
-	void copyBitmap(const uint8_t* srcImg, const FrustumType::Vector& srcSize,
-		const FrustumType::Vector& pos);
-
-	/// Compute image size (imgSize) using the glyphs set
-	void computeImageSize();
-
-	/// Given a filename and a font size create an image with all the glyphs
-	void createImage(const char* filename, const FrustumType::Vector& fontSize);
-};
-
-} // end namespace
-
-#endif

+ 68 - 0
include/anki/ui/UiInterface.h

@@ -0,0 +1,68 @@
+// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include "anki/ui/UiObject.h"
+#include "anki/input/KeyCode.h"
+
+namespace anki {
+
+/// @addtogroup ui
+/// @{
+
+/// UI image interface.
+class IImage: public UiObject
+{
+	// Empty.
+};
+
+/// Interfacing UI system with external systems.
+class UiInterface
+{
+public:
+	virtual ~UiInterface() = default;
+
+	/// @name Image related methods.
+	/// @{
+	virtual ANKI_USE_RESULT Error loadImage(
+		const CString& filename, ImagePtr& img) = 0;
+
+	virtual ANKI_USE_RESULT Error createRgba8Image(
+		const void* data, PtrSize dataSize, const Vec2& size,
+		ImagePtr& img) = 0;
+	/// @}
+
+	/// @name Misc methods.
+	/// @{
+	virtual ANKI_USE_RESULT Error readFile(const CString& filename,
+		DArrayAuto<U8>& data) = 0;
+	/// @}
+
+	/// @name Painting related methods.
+	/// @{
+	virtual void drawImage(ImagePtr image, const Rect& subImageRect,
+		const Rect& drawingRect) = 0;
+
+	virtual void drawText(const CString& text, const Rect& drawingRect,
+		const Color& color) = 0;
+
+	virtual void drawLines(const SArray<Vec2>& lines, const Color& color) = 0;
+	/// @}
+
+	/// @name Input related methods
+	/// @{
+	virtual void injectMouseMove(const Vec2& pos) = 0;
+
+	virtual void injectKeyDown(KeyCode key) = 0;
+
+	virtual void injectKeyUp(KeyCode key) = 0;
+
+	virtual void injectMouseButtonDown(U button) = 0;
+	/// @}
+};
+/// @}
+
+} // end namespace anki

+ 39 - 0
include/anki/ui/UiObject.h

@@ -0,0 +1,39 @@
+// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include "anki/ui/Common.h"
+
+namespace anki {
+
+/// @addtogroup ui
+/// @{
+
+/// The base of all UI objects.
+class UiObject
+{
+public:
+	UiObject(Canvas* canvas)
+		: m_canvas(canvas)
+	{}
+
+	virtual ~UiObject() = default;
+
+	Atomic<I32>& getRefcount()
+	{
+		return m_refcount;
+	}
+
+	UiAllocator getAllocator() const;
+
+private:
+	Atomic<I32> m_refcount = {0};
+	Canvas* m_canvas = nullptr;
+};
+/// @}
+
+} // end namespace anki
+

+ 0 - 127
include/anki/ui/UiPainter.h

@@ -1,127 +0,0 @@
-// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#ifndef ANKI_UI_UI_PAINTER_H
-#define ANKI_UI_UI_PAINTER_H
-
-#include "anki/ui/UiCommon.h"
-#include "anki/gr/Gl.h"
-
-namespace anki {
-
-// Forward
-class UiFont;
-
-/// Gradient type
-struct UiGrandient
-{
-	enum GradientType
-	{
-		GT_ANGULAR,
-		GT_RADIAL
-	};
-
-	GradientType type = GT_ANGULAR;
-	Array<UiColor, 3> colors = {{UiColor(1.0), UiColor(0.5), UiColor(0.0)}};
-	U8 colorsCount = 2;
-	F32 angle = 0.0; ///< In rad. Default is horizontal
-};
-
-/// UI painer
-class UiPainter
-{
-public:
-	/// Method for coloring
-	enum ColoringMethod
-	{
-		CM_CONSTANT_COLOR,
-		CM_GRADIENT
-	};
-
-	UiPainter(const Vec2& deviceSize);
-
-	/// @name Accessors
-	/// @{
-	const UiPosition& getPosition() const
-	{
-		return pos;
-	}
-	void setPosition(const UiPosition& x)
-	{
-		pos = x;
-	}
-
-	const UiColor& getColor() const
-	{
-		return col;
-	}
-	void setColor(const UiColor& x)
-	{
-		col = x;
-	}
-	/// @}
-
-	/// @name Fill methods
-	/// @{
-	void fillRect(const UiRect& rect);
-	void fillEllipse(const UiRect& rect);
-	void fillRoundedRect(const UiRect& rect, F32 xRadius, F32 yRadius);
-	/// @}
-
-	/// @name Draw methods
-	/// @{
-	void drawText(const char* text);
-	void drawFormatedText(const char* format, ...);
-
-	void drawLines(const UiPosition* positions, U32 positionsCount);
-	void drawRect(const UiRect& rect);
-	void drawEllipse(const UiRect& rect);
-	void drawRoundedRect(const UiRect& rect, F32 xRadius, F32 yRadius);
-	/// @}
-
-private:
-	enum Shader
-	{
-		S_FILL,
-		S_GRADIENT_ANGULAR_COLORS_2,
-		S_GRADIENT_ANGULAR_COLORS_3,
-		S_GRADIENT_RADIAL_COLORS_2,
-		S_GRADIENT_RADIAL_COLORS_3,
-		S_COUNT
-	};
-
-	/// @name Data
-	/// @{
-	UiFont* font = nullptr;
-	Array<ShaderProgram, S_COUNT> progs;
-
-	Vbo qPositionsVbo;
-	Vbo qIndecesVbo;
-	Vao qVao;
-
-	Vec2 deviceSize; ///< The size of the device in pixels
-	/// @}
-
-	/// @name State machine data
-	/// @{
-	UiPosition pos;
-	UiColor col;
-	Gradient grad;
-	ColoringMethod fillMethod;
-	ColoringMethod linesMethod;
-	/// @}
-
-	/// @name Oprtimizations
-	/// @{
-	/// @}
-
-	void init();
-};
-
-
-} // end namespace
-
-
-#endif

+ 0 - 36
include/anki/ui/UiPainterDevice.h

@@ -1,36 +0,0 @@
-// Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#ifndef ANKI_UI_UI_PAINTER_DEVICE_H
-#define ANKI_UI_UI_PAINTER_DEVICE_H
-
-#include "anki/gr/Fbo.h"
-#include "anki/Math.h"
-
-namespace anki {
-
-class Texture;
-
-/// This actually and FBO but with size info
-class UiPainterDevice: public Fbo
-{
-public:
-	/// Constructor
-	UiPainterDevice(Texture* colorFai);
-
-	/// @name Accessors
-	/// @{
-	Vec2 getSize() const;
-	/// @}
-
-private:
-	Texture* colorFai;
-
-	void create();
-};
-
-} // end namespace anki
-
-#endif

+ 18 - 6
include/anki/ui/UiFactory.h → include/anki/ui/Widget.h

@@ -3,19 +3,31 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
-#ifndef ANKI_UI_UI_FACTORY_H
-#define ANKI_UI_UI_FACTORY_H
+#pragma once
 
-#include "anki/ui/UiCommon.h"
+#include "anki/ui/Common.h"
 
 namespace anki {
 
-class UiFactory
+/// @addtogroup ui
+/// @{
+
+enum class WidgetState: U8
 {
-private:
+	DEFAULT,
+	HOVER,
+	ACTIVATED,
+	VISIBLE,
+	ENABLED
+};
 
+/// UI widget.
+class Widget
+{
+public:
+private:
 };
+/// @}
 
 } // end namespace anki
 
-#endif