3D scene toolbar uses a single texture

This commit is contained in:
Enrico Turri 2018-07-31 12:25:00 +02:00
parent 55e7a1af46
commit 1c0fa19824
72 changed files with 690 additions and 271 deletions

View file

@ -28,41 +28,38 @@ public:
enum EState : unsigned char
{
Normal,
Hover,
Pressed,
HoverPressed,
Disabled,
Hover,
HoverPressed,
Num_States
};
private:
// icon textures are assumed to be square and all with the same size in pixels, no internal check is done
GLTexture m_icon_textures[Num_States];
struct Data
{
std::string name;
std::string tooltip;
unsigned int sprite_id;
bool is_toggable;
PerlCallback* action_callback;
Data();
};
private:
EType m_type;
EState m_state;
std::string m_name;
std::string m_tooltip;
bool m_is_toggable;
PerlCallback* m_action_callback;
Data m_data;
public:
GLToolbarItem(EType type, const std::string& name, const std::string& tooltip, bool is_toggable, PerlCallback* action_callback);
bool load_textures(const std::string* filenames);
GLToolbarItem(EType type, const Data& data);
EState get_state() const;
void set_state(EState state);
const std::string& get_name() const;
const std::string& get_tooltip() const;
unsigned int get_icon_texture_id() const;
int get_icon_textures_size() const;
void do_action();
bool is_enabled() const;
@ -71,18 +68,48 @@ public:
bool is_toggable() const;
bool is_separator() const;
void render(unsigned int tex_id, float left, float right, float bottom, float top, unsigned int texture_size, unsigned int border_size, unsigned int icon_size, unsigned int gap_size) const;
private:
GLTexture::Quad_UVs _get_uvs(unsigned int texture_size, unsigned int border_size, unsigned int icon_size, unsigned int gap_size) const;
};
class GLToolbar
{
public:
struct ItemCreationData
// items icon textures are assumed to be square and all with the same size in pixels, no internal check is done
// icons are layed-out into the texture starting from the top-left corner in the same order as enum GLToolbarItem::EState
// from left to right
struct ItemsIconsTexture
{
std::string name;
std::string tooltip;
bool is_toggable;
PerlCallback* action_callback;
std::string textures[GLToolbarItem::Num_States];
GLTexture texture;
// size of the square icons, in pixels
unsigned int items_icon_size;
// distance from the border, in pixels
unsigned int items_icon_border_size;
// distance between two adjacent icons (to avoid filtering artifacts), in pixels
unsigned int items_icon_gap_size;
ItemsIconsTexture();
};
struct Layout
{
enum Type : unsigned char
{
Horizontal,
Vertical,
Num_Types
};
Type type;
float top;
float left;
float separator_size;
float gap_size;
Layout();
};
private:
@ -90,27 +117,32 @@ private:
GLCanvas3D& m_parent;
bool m_enabled;
ItemsList m_items;
ItemsIconsTexture m_icons_texture;
Layout m_layout;
float m_textures_scale;
float m_offset_y;
float m_gap_x;
float m_separator_x;
ItemsList m_items;
public:
explicit GLToolbar(GLCanvas3D& parent);
bool init(const std::string& icons_texture_filename, unsigned int items_icon_size, unsigned int items_icon_border_size, unsigned int items_icon_gap_size);
Layout::Type get_layout_type() const;
void set_layout_type(Layout::Type type);
void set_position(float top, float left);
void set_separator_size(float size);
void set_gap_size(float size);
bool is_enabled() const;
void set_enabled(bool enable);
void set_textures_scale(float scale);
void set_offset_y(float offset);
void set_gap_x(float gap);
void set_separator_x(float separator);
bool add_item(const ItemCreationData& data);
bool add_item(const GLToolbarItem::Data& data);
bool add_separator();
float get_width() const;
float get_height() const;
void enable_item(const std::string& name);
void disable_item(const std::string& name);
@ -123,10 +155,20 @@ public:
void do_action(unsigned int item_id);
void render(const Pointf& mouse_pos) const;
void render() const;
private:
float _get_total_width() const;
float _get_width_horizontal() const;
float _get_width_vertical() const;
float _get_height_horizontal() const;
float _get_height_vertical() const;
float _get_main_size() const;
void _update_hover_state_horizontal(const Pointf& mouse_pos);
void _update_hover_state_vertical(const Pointf& mouse_pos);
int _contains_mouse_horizontal(const Pointf& mouse_pos) const;
int _contains_mouse_vertical(const Pointf& mouse_pos) const;
void _render_horizontal() const;
void _render_vertical() const;
};
} // namespace GUI