2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UI_WIDGET_H__
|
|
|
|
#define __UI_WIDGET_H__
|
|
|
|
|
|
|
|
#include "ui.hpp"
|
|
|
|
#include "ui_text.hpp"
|
|
|
|
#include "ui_painter.hpp"
|
|
|
|
#include "ui_focus.hpp"
|
|
|
|
|
|
|
|
#include "utility.hpp"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2015-08-15 00:17:23 -04:00
|
|
|
void dirty_set();
|
|
|
|
void dirty_clear();
|
|
|
|
bool is_dirty();
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-11-09 14:56:40 -05:00
|
|
|
class Context {
|
|
|
|
public:
|
|
|
|
FocusManager& focus_manager() {
|
|
|
|
return focus_manager_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-26 19:50:44 -05:00
|
|
|
FocusManager focus_manager_ { };
|
2015-07-08 11:39:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class Widget {
|
|
|
|
public:
|
2016-04-22 14:18:35 -04:00
|
|
|
Widget(
|
2016-12-06 12:28:48 -05:00
|
|
|
) : _parent_rect { }
|
2015-07-08 11:39:24 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-22 14:18:35 -04:00
|
|
|
Widget(
|
2015-07-08 11:39:24 -04:00
|
|
|
Rect parent_rect
|
2016-12-06 12:28:48 -05:00
|
|
|
) : _parent_rect { parent_rect }
|
2015-07-08 11:39:24 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget(const Widget&) = delete;
|
|
|
|
Widget(Widget&&) = delete;
|
2016-11-26 19:52:57 -05:00
|
|
|
Widget& operator=(const Widget&) = delete;
|
|
|
|
Widget& operator=(Widget&&) = delete;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
virtual ~Widget() = default;
|
|
|
|
|
|
|
|
Point screen_pos();
|
|
|
|
Size size() const;
|
2016-05-08 17:59:26 -04:00
|
|
|
Rect screen_rect() const;
|
2016-12-06 12:28:48 -05:00
|
|
|
Rect parent_rect() const;
|
2015-07-08 11:39:24 -04:00
|
|
|
virtual void set_parent_rect(const Rect new_parent_rect);
|
|
|
|
|
|
|
|
Widget* parent() const;
|
|
|
|
void set_parent(Widget* const widget);
|
|
|
|
|
|
|
|
bool hidden() const { return flags.hidden; }
|
|
|
|
void hidden(bool hide);
|
|
|
|
|
|
|
|
virtual void focus();
|
|
|
|
virtual void on_focus();
|
|
|
|
virtual void blur();
|
|
|
|
virtual void on_blur();
|
|
|
|
bool focusable() const;
|
2016-02-07 13:32:38 -05:00
|
|
|
void set_focusable(const bool value);
|
2015-07-08 11:39:24 -04:00
|
|
|
bool has_focus();
|
|
|
|
|
|
|
|
virtual void paint(Painter& painter) = 0;
|
|
|
|
|
|
|
|
virtual void on_show() { };
|
|
|
|
virtual void on_hide() { };
|
|
|
|
|
|
|
|
virtual bool on_key(const KeyEvent event);
|
|
|
|
virtual bool on_encoder(const EncoderEvent event);
|
|
|
|
virtual bool on_touch(const TouchEvent event);
|
2016-02-07 14:24:25 -05:00
|
|
|
virtual const std::vector<Widget*>& children() const;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
virtual Context& context() const;
|
|
|
|
|
|
|
|
void set_style(const Style* new_style);
|
|
|
|
|
|
|
|
const Style& style() const;
|
|
|
|
|
|
|
|
// State management methods.
|
|
|
|
void set_dirty();
|
|
|
|
bool dirty() const;
|
|
|
|
void set_clean();
|
|
|
|
|
|
|
|
void visible(bool v);
|
|
|
|
|
2016-02-07 13:32:38 -05:00
|
|
|
bool highlighted() const;
|
|
|
|
void set_highlighted(const bool value);
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
protected:
|
2016-02-07 13:40:06 -05:00
|
|
|
void dirty_overlapping_children_in_rect(const Rect& child_rect);
|
|
|
|
|
|
|
|
private:
|
2015-07-08 11:39:24 -04:00
|
|
|
/* Widget rectangle relative to parent pos(). */
|
2016-12-06 12:28:48 -05:00
|
|
|
Rect _parent_rect;
|
2015-07-08 11:39:24 -04:00
|
|
|
const Style* style_ { nullptr };
|
|
|
|
Widget* parent_ { nullptr };
|
|
|
|
|
|
|
|
struct flags_t {
|
|
|
|
bool dirty : 1; // Widget content has changed.
|
|
|
|
bool hidden : 1; // Hide widget and children.
|
|
|
|
bool focusable : 1; // Widget can receive focus.
|
|
|
|
bool highlighted : 1; // Show in a highlighted style.
|
|
|
|
bool visible : 1; // Object was visible during last paint.
|
|
|
|
};
|
|
|
|
|
|
|
|
flags_t flags {
|
|
|
|
.dirty = true,
|
|
|
|
.hidden = false,
|
|
|
|
.focusable = false,
|
|
|
|
.highlighted = false,
|
|
|
|
.visible = false,
|
|
|
|
};
|
2016-02-07 14:24:25 -05:00
|
|
|
|
|
|
|
static const std::vector<Widget*> no_children;
|
2015-07-08 11:39:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class View : public Widget {
|
|
|
|
public:
|
|
|
|
View() {
|
|
|
|
}
|
|
|
|
|
|
|
|
View(Rect parent_rect) {
|
|
|
|
set_parent_rect(parent_rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ~View() should on_hide() all children?
|
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
|
|
|
void add_child(Widget* const widget);
|
2016-09-05 17:53:04 -04:00
|
|
|
void add_children(const std::initializer_list<Widget*> children);
|
2015-07-08 11:39:24 -04:00
|
|
|
void remove_child(Widget* const widget);
|
2016-02-07 14:24:25 -05:00
|
|
|
const std::vector<Widget*>& children() const override;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2016-01-26 16:01:16 -05:00
|
|
|
virtual std::string title() const;
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
protected:
|
2016-11-26 19:50:44 -05:00
|
|
|
std::vector<Widget*> children_ { };
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
void invalidate_child(Widget* const widget);
|
|
|
|
};
|
|
|
|
|
|
|
|
class Rectangle : public Widget {
|
|
|
|
public:
|
2016-05-25 14:32:06 -04:00
|
|
|
Rectangle(Color c);
|
2015-12-17 01:35:26 -05:00
|
|
|
Rectangle(Rect parent_rect, Color c);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
|
|
|
void set_color(const Color c);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Color color;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Text : public Widget {
|
|
|
|
public:
|
|
|
|
Text(
|
|
|
|
) : text { "" } {
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:35:26 -05:00
|
|
|
Text(Rect parent_rect, std::string text);
|
|
|
|
Text(Rect parent_rect);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
void set(const std::string value);
|
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string text;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Button : public Widget {
|
|
|
|
public:
|
2016-11-26 19:50:44 -05:00
|
|
|
std::function<void(Button&)> on_select { };
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-12-17 01:35:26 -05:00
|
|
|
Button(Rect parent_rect, std::string text);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
Button(
|
|
|
|
) : Button { { }, { } }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_text(const std::string value);
|
|
|
|
std::string text() const;
|
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
|
|
|
bool on_key(const KeyEvent key) override;
|
|
|
|
bool on_touch(const TouchEvent event) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string text_;
|
|
|
|
};
|
|
|
|
|
2016-02-03 18:48:50 -05:00
|
|
|
class Image : public Widget {
|
|
|
|
public:
|
|
|
|
Image();
|
|
|
|
Image(
|
|
|
|
const Rect parent_rect,
|
|
|
|
const Bitmap* bitmap,
|
|
|
|
const Color foreground,
|
|
|
|
const Color background
|
|
|
|
);
|
|
|
|
|
2016-11-26 19:52:57 -05:00
|
|
|
Image(const Image&) = delete;
|
|
|
|
Image(Image&&) = delete;
|
|
|
|
Image& operator=(const Image&) = delete;
|
|
|
|
Image& operator=(Image&&) = delete;
|
|
|
|
|
2016-02-03 18:48:50 -05:00
|
|
|
void set_bitmap(const Bitmap* bitmap);
|
|
|
|
void set_foreground(const Color color);
|
|
|
|
void set_background(const Color color);
|
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Bitmap* bitmap_;
|
|
|
|
Color foreground_;
|
|
|
|
Color background_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ImageButton : public Image {
|
|
|
|
public:
|
2016-11-26 19:50:44 -05:00
|
|
|
std::function<void(ImageButton&)> on_select { };
|
2016-02-03 18:48:50 -05:00
|
|
|
|
|
|
|
ImageButton(
|
|
|
|
const Rect parent_rect,
|
|
|
|
const Bitmap* bitmap,
|
|
|
|
const Color foreground,
|
|
|
|
const Color background
|
|
|
|
);
|
|
|
|
|
|
|
|
bool on_key(const KeyEvent key) override;
|
|
|
|
bool on_touch(const TouchEvent event) override;
|
|
|
|
};
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
class OptionsField : public Widget {
|
|
|
|
public:
|
|
|
|
using name_t = std::string;
|
|
|
|
using value_t = int32_t;
|
|
|
|
using option_t = std::pair<name_t, value_t>;
|
|
|
|
using options_t = std::vector<option_t>;
|
|
|
|
|
2016-11-26 19:50:44 -05:00
|
|
|
std::function<void(size_t, value_t)> on_change { };
|
|
|
|
std::function<void(void)> on_show_options { };
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
OptionsField(Point parent_pos, int length, options_t options);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
size_t selected_index() const;
|
|
|
|
void set_selected_index(const size_t new_index);
|
|
|
|
|
|
|
|
void set_by_value(value_t v);
|
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
2016-02-02 16:41:11 -05:00
|
|
|
void on_focus() override;
|
2015-07-08 11:39:24 -04:00
|
|
|
bool on_encoder(const EncoderEvent delta) override;
|
|
|
|
bool on_touch(const TouchEvent event) override;
|
|
|
|
|
|
|
|
private:
|
2016-11-29 13:13:56 -05:00
|
|
|
const int length_;
|
2015-07-08 11:39:24 -04:00
|
|
|
options_t options;
|
|
|
|
size_t selected_index_ { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
class NumberField : public Widget {
|
|
|
|
public:
|
2016-11-26 19:50:44 -05:00
|
|
|
std::function<void(int32_t)> on_change { };
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
using range_t = std::pair<int32_t, int32_t>;
|
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
NumberField(Point parent_pos, int length, range_t range, int32_t step, char fill_char);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
NumberField(const NumberField&) = delete;
|
|
|
|
NumberField(NumberField&&) = delete;
|
|
|
|
|
|
|
|
int32_t value() const;
|
|
|
|
void set_value(int32_t new_value);
|
|
|
|
|
|
|
|
void paint(Painter& painter) override;
|
|
|
|
|
|
|
|
bool on_encoder(const EncoderEvent delta) override;
|
|
|
|
bool on_touch(const TouchEvent event) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const range_t range;
|
|
|
|
const int32_t step;
|
2016-11-29 13:13:56 -05:00
|
|
|
const int length_;
|
2015-07-08 11:39:24 -04:00
|
|
|
const char fill_char;
|
2016-11-26 19:50:44 -05:00
|
|
|
int32_t value_ { 0 };
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
int32_t clip_value(int32_t value);
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace ui */
|
|
|
|
|
|
|
|
#endif/*__UI_WIDGET_H__*/
|