Restructuring for CVar-enabled controls

This commit is contained in:
Jack Andersen 2015-12-05 15:24:51 -10:00
parent fc67d86b29
commit 21e671e36f
11 changed files with 261 additions and 34 deletions

View File

@ -51,7 +51,8 @@ list(APPEND SPECTER_HEADERS
include/Specter/Menu.hpp include/Specter/Menu.hpp
include/Specter/Node.hpp include/Specter/Node.hpp
include/Specter/NodeSocket.hpp include/Specter/NodeSocket.hpp
include/Specter/FontCache.hpp) include/Specter/FontCache.hpp
include/Specter/Translator.hpp)
atdna(atdna_FontCache.cpp include/Specter/FontCache.hpp) atdna(atdna_FontCache.cpp include/Specter/FontCache.hpp)
@ -77,6 +78,7 @@ list(APPEND SPECTER_SOURCES
lib/Node.cpp lib/Node.cpp
lib/NodeSocket.cpp lib/NodeSocket.cpp
lib/FontCache.cpp lib/FontCache.cpp
lib/Translator.cpp
atdna_FontCache.cpp) atdna_FontCache.cpp)
add_library(Specter ${SPECTER_SOURCES} ${SPECTER_HEADERS}) add_library(Specter ${SPECTER_SOURCES} ${SPECTER_HEADERS})

View File

@ -2,12 +2,12 @@
#define SPECTER_BUTTON_HPP #define SPECTER_BUTTON_HPP
#include "Specter/TextView.hpp" #include "Specter/TextView.hpp"
#include "Specter/Button.hpp" #include "Specter/Control.hpp"
namespace Specter namespace Specter
{ {
class Button : public View class Button : public Control
{ {
std::string m_textStr; std::string m_textStr;
std::unique_ptr<TextView> m_text; std::unique_ptr<TextView> m_text;
@ -37,7 +37,8 @@ public:
void init(boo::IGraphicsDataFactory* factory, const ThemeData& theme); void init(boo::IGraphicsDataFactory* factory, const ThemeData& theme);
}; };
Button(ViewResources& res, View& parentView, const std::string& text); Button(ViewResources& res, View& parentView,
std::unique_ptr<IControlBinding>&& controlBinding, const std::string& text);
void mouseDown(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey); void mouseDown(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseUp(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey); void mouseUp(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseEnter(const boo::SWindowCoord&); void mouseEnter(const boo::SWindowCoord&);

View File

@ -1,4 +1,41 @@
#ifndef SPECTER_CONTROL_HPP #ifndef SPECTER_CONTROL_HPP
#define SPECTER_CONTROL_HPP #define SPECTER_CONTROL_HPP
#include "View.hpp"
#include "HECL/CVar.hpp"
namespace Specter
{
struct IControlBinding
{
virtual const std::string& name() const=0;
virtual const std::string& help() const=0;
};
struct CVarControlBinding : IControlBinding
{
HECL::CVar* m_cvar;
CVarControlBinding(HECL::CVar* cvar)
: m_cvar(cvar) {}
const std::string& name() const {return m_cvar->name();}
const std::string& help() const {return m_cvar->rawHelp();}
};
class Control : public View
{
protected:
std::unique_ptr<IControlBinding> m_controlBinding;
public:
Control(ViewResources& res, View& parentView, std::unique_ptr<IControlBinding>&& controlBinding);
void mouseDown(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseUp(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseEnter(const boo::SWindowCoord&);
void mouseLeave(const boo::SWindowCoord&);
void setControlBinding(std::unique_ptr<IControlBinding>&& controlBinding);
};
}
#endif // SPECTER_CONTROL_HPP #endif // SPECTER_CONTROL_HPP

View File

@ -1,4 +1,56 @@
#ifndef SPECTER_NUMERICFIELD_HPP #ifndef SPECTER_NUMERICFIELD_HPP
#define SPECTER_NUMERICFIELD_HPP #define SPECTER_NUMERICFIELD_HPP
#include "Specter/TextView.hpp"
namespace Specter
{
class ViewResources;
class NumericField : public View
{
std::string m_textStr;
std::unique_ptr<TextView> m_text;
SolidShaderVert m_verts[28];
ViewBlock m_bBlock;
boo::IGraphicsBufferD* m_bBlockBuf;
boo::IGraphicsBufferD* m_bVertsBuf;
boo::IVertexFormat* m_bVtxFmt; /* OpenGL only */
boo::IShaderDataBinding* m_bShaderBinding;
int m_nomWidth, m_nomHeight;
bool m_pressed = false;
bool m_hovered = false;
void setInactive();
void setHover();
void setPressed();
void setDisabled();
public:
class Resources
{
friend class ViewResources;
friend class Button;
void init(boo::IGraphicsDataFactory* factory, const ThemeData& theme);
};
NumericField(ViewResources& res, View& parentView, const std::string& text);
void mouseDown(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseUp(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseEnter(const boo::SWindowCoord&);
void mouseLeave(const boo::SWindowCoord&);
void resized(const boo::SWindowRect& rootView, const boo::SWindowRect& sub);
void resetResources(ViewResources& res);
void draw(boo::IGraphicsCommandQueue* gfxQ);
void setText(const std::string& text);
int nominalWidth() const {return m_nomWidth;}
int nominalHeight() const {return m_nomHeight;}
};
}
#endif // SPECTER_NUMERICFIELD_HPP #endif // SPECTER_NUMERICFIELD_HPP

View File

@ -12,6 +12,10 @@
namespace Specter namespace Specter
{ {
struct IViewManager
{
};
class RootView : public View class RootView : public View
{ {
boo::IWindow* m_window = nullptr; boo::IWindow* m_window = nullptr;
@ -19,12 +23,13 @@ class RootView : public View
boo::SWindowRect m_rootRect = {}; boo::SWindowRect m_rootRect = {};
bool m_resizeRTDirty = false; bool m_resizeRTDirty = false;
bool m_destroyed = false; bool m_destroyed = false;
IViewManager& m_viewMan;
ViewResources* m_viewRes; ViewResources* m_viewRes;
DeferredWindowEvents<RootView> m_events; DeferredWindowEvents<RootView> m_events;
public: public:
RootView(ViewResources& res, boo::IWindow* window); RootView(IViewManager& viewMan, ViewResources& res, boo::IWindow* window);
void destroyed(); void destroyed();
bool isDestroyed() const {return m_destroyed;} bool isDestroyed() const {return m_destroyed;}
@ -54,11 +59,16 @@ public:
const boo::SWindowRect& rootRect() const {return m_rootRect;} const boo::SWindowRect& rootRect() const {return m_rootRect;}
boo::IWindow* window() const {return m_window;} boo::IWindow* window() const {return m_window;}
IViewManager& viewManager() const {return m_viewMan;}
const ViewResources& viewRes() const {return *m_viewRes;} const ViewResources& viewRes() const {return *m_viewRes;}
const ThemeData& themeData() const {return m_viewRes->m_theme;} const ThemeData& themeData() const {return m_viewRes->m_theme;}
void setContentView(std::unique_ptr<View>&& view);
void displayTooltip(const std::string& name, const std::string& help);
private: private:
std::unique_ptr<SplitView> m_splitView; std::unique_ptr<View> m_view;
}; };
} }

View File

@ -0,0 +1,34 @@
#ifndef SPECTER_TRANSLATOR_HPP
#define SPECTER_TRANSLATOR_HPP
#include <string>
#include <Athena/DNAYaml.hpp>
namespace Specter
{
class Locale
{
std::string m_name;
std::string m_fullName;
std::unique_ptr<Athena::io::YAMLNode> m_rootNode;
public:
Locale(const std::string& name, const std::string& fullName,
const unsigned char* yamlSource, size_t yamlLength);
const std::string& name() const {return m_name;}
const std::string& fullName() const {return m_fullName;}
const Athena::io::YAMLNode& rootNode() const {return *m_rootNode;}
};
class Translator
{
const Locale* m_targetLocale;
public:
Translator(const Locale* targetLocale) {setLocale(targetLocale);}
void setLocale(const Locale* targetLocale);
const std::string* translate(const std::string& key);
};
}
#endif // SPECTER_TRANSLATOR_HPP

View File

@ -73,6 +73,7 @@ public:
void init(boo::IGraphicsDataFactory* factory, FontCache* fcache, const ThemeData& theme, unsigned dpi); void init(boo::IGraphicsDataFactory* factory, FontCache* fcache, const ThemeData& theme, unsigned dpi);
void resetDPI(unsigned dpi); void resetDPI(unsigned dpi);
void resetTheme(const ThemeData& theme); void resetTheme(const ThemeData& theme);
void resetLanguage(const ThemeData& theme);
float m_pixelFactor = 0; float m_pixelFactor = 0;
float pixelFactor() const {return m_pixelFactor;} float pixelFactor() const {return m_pixelFactor;}

View File

@ -11,8 +11,9 @@ void Button::Resources::init(boo::IGraphicsDataFactory* factory, const ThemeData
{ {
} }
Button::Button(ViewResources& res, View& parentView, const std::string& text) Button::Button(ViewResources& res, View& parentView,
: View(res, parentView), m_textStr(text) std::unique_ptr<IControlBinding>&& controlBinding, const std::string& text)
: Control(res, parentView, std::move(controlBinding)), m_textStr(text)
{ {
m_bBlockBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Uniform, sizeof(ViewBlock), 1); m_bBlockBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Uniform, sizeof(ViewBlock), 1);
m_bVertsBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SolidShaderVert), 28); m_bVertsBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SolidShaderVert), 28);
@ -131,12 +132,14 @@ void Button::setDisabled()
void Button::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod) void Button::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{ {
Control::mouseDown(coord, button, mod);
m_pressed = true; m_pressed = true;
setPressed(); setPressed();
} }
void Button::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod) void Button::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{ {
Control::mouseUp(coord, button, mod);
if (m_pressed && m_hovered) if (m_pressed && m_hovered)
Log.report(LogVisor::Info, "button '%s' activated", m_textStr.c_str()); Log.report(LogVisor::Info, "button '%s' activated", m_textStr.c_str());
m_pressed = false; m_pressed = false;
@ -148,6 +151,7 @@ void Button::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, b
void Button::mouseEnter(const boo::SWindowCoord& coord) void Button::mouseEnter(const boo::SWindowCoord& coord)
{ {
Control::mouseEnter(coord);
m_hovered = true; m_hovered = true;
if (m_pressed) if (m_pressed)
setPressed(); setPressed();
@ -157,6 +161,7 @@ void Button::mouseEnter(const boo::SWindowCoord& coord)
void Button::mouseLeave(const boo::SWindowCoord& coord) void Button::mouseLeave(const boo::SWindowCoord& coord)
{ {
Control::mouseLeave(coord);
m_hovered = false; m_hovered = false;
setInactive(); setInactive();
} }

View File

@ -0,0 +1,31 @@
#include "Specter/Control.hpp"
namespace Specter
{
Control::Control(ViewResources& res, View& parentView,
std::unique_ptr<IControlBinding>&& controlBinding)
: View(res, parentView), m_controlBinding(std::move(controlBinding)) {}
void Control::setControlBinding(std::unique_ptr<IControlBinding>&& controlBinding)
{
m_controlBinding = std::move(controlBinding);
}
void Control::mouseDown(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey)
{
}
void Control::mouseUp(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey)
{
}
void Control::mouseEnter(const boo::SWindowCoord&)
{
}
void Control::mouseLeave(const boo::SWindowCoord&)
{
}
}

View File

@ -6,30 +6,14 @@ namespace Specter
{ {
static LogVisor::LogModule Log("Specter::RootView"); static LogVisor::LogModule Log("Specter::RootView");
RootView::RootView(ViewResources& res, boo::IWindow* window) RootView::RootView(IViewManager& viewMan, ViewResources& res, boo::IWindow* window)
: View(res), m_window(window), m_events(*this), m_viewRes(&res) : View(res), m_window(window), m_events(*this), m_viewMan(viewMan), m_viewRes(&res)
{ {
window->setCallback(&m_events); window->setCallback(&m_events);
boo::SWindowRect rect = window->getWindowFrame(); boo::SWindowRect rect = window->getWindowFrame();
m_renderTex = res.m_factory->newRenderTexture(rect.size[0], rect.size[1], 1); m_renderTex = res.m_factory->newRenderTexture(rect.size[0], rect.size[1], 1);
commitResources(res); commitResources(res);
m_splitView.reset(new SplitView(res, *this, SplitView::Axis::Horizontal));
Space* space1 = new Space(res, *m_splitView, Toolbar::Position::Top);
space1->toolbar().push_back(std::make_unique<Button>(res, space1->toolbar(), "Hello Button"));
MultiLineTextView* textView1 = new MultiLineTextView(res, *this, res.m_heading18);
space1->setContentView(std::unique_ptr<MultiLineTextView>(textView1));
Space* space2 = new Space(res, *m_splitView, Toolbar::Position::Bottom);
space2->toolbar().push_back(std::make_unique<Button>(res, space2->toolbar(), "こんにちはボタン"));
MultiLineTextView* textView2 = new MultiLineTextView(res, *this, res.m_heading18);
space2->setContentView(std::unique_ptr<MultiLineTextView>(textView2));
m_splitView->setContentView(0, std::unique_ptr<Space>(space1));
m_splitView->setContentView(1, std::unique_ptr<Space>(space2));
resized(rect, rect); resized(rect, rect);
textView1->typesetGlyphs("Hello, World!\n\n", res.themeData().uiText());
textView2->typesetGlyphs("こんにちは世界!\n\n", res.themeData().uiText());
textView1->setBackground(res.themeData().viewportBackground());
textView2->setBackground(res.themeData().viewportBackground());
setBackground(Zeus::CColor::skGrey);
} }
void RootView::destroyed() void RootView::destroyed()
@ -44,34 +28,40 @@ void RootView::resized(const boo::SWindowRect& root, const boo::SWindowRect&)
m_rootRect.location[0] = 0; m_rootRect.location[0] = 0;
m_rootRect.location[1] = 0; m_rootRect.location[1] = 0;
View::resized(m_rootRect, m_rootRect); View::resized(m_rootRect, m_rootRect);
m_splitView->resized(m_rootRect, m_rootRect); if (m_view)
m_view->resized(m_rootRect, m_rootRect);
if (old != m_rootRect) if (old != m_rootRect)
m_resizeRTDirty = true; m_resizeRTDirty = true;
} }
void RootView::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mods) void RootView::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mods)
{ {
m_splitView->mouseDown(coord, button, mods); if (m_view)
m_view->mouseDown(coord, button, mods);
} }
void RootView::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mods) void RootView::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mods)
{ {
m_splitView->mouseUp(coord, button, mods); if (m_view)
m_view->mouseUp(coord, button, mods);
} }
void RootView::mouseMove(const boo::SWindowCoord& coord) void RootView::mouseMove(const boo::SWindowCoord& coord)
{ {
m_splitView->mouseMove(coord); if (m_view)
m_view->mouseMove(coord);
} }
void RootView::mouseEnter(const boo::SWindowCoord& coord) void RootView::mouseEnter(const boo::SWindowCoord& coord)
{ {
m_splitView->mouseEnter(coord); if (m_view)
m_view->mouseEnter(coord);
} }
void RootView::mouseLeave(const boo::SWindowCoord& coord) void RootView::mouseLeave(const boo::SWindowCoord& coord)
{ {
m_splitView->mouseLeave(coord); if (m_view)
m_view->mouseLeave(coord);
} }
void RootView::scroll(const boo::SWindowCoord& coord, const boo::SScrollDelta& scroll) void RootView::scroll(const boo::SWindowCoord& coord, const boo::SScrollDelta& scroll)
@ -119,7 +109,18 @@ void RootView::modKeyUp(boo::EModifierKey mod)
void RootView::resetResources(ViewResources& res) void RootView::resetResources(ViewResources& res)
{ {
m_viewRes = &res; m_viewRes = &res;
m_splitView->resetResources(res); if (m_view)
m_view->resetResources(res);
}
void RootView::setContentView(std::unique_ptr<View>&& view)
{
m_view = std::move(view);
updateSize();
}
void RootView::displayTooltip(const std::string& name, const std::string& help)
{
} }
void RootView::draw(boo::IGraphicsCommandQueue* gfxQ) void RootView::draw(boo::IGraphicsCommandQueue* gfxQ)
@ -133,7 +134,8 @@ void RootView::draw(boo::IGraphicsCommandQueue* gfxQ)
gfxQ->setViewport(m_rootRect); gfxQ->setViewport(m_rootRect);
gfxQ->setScissor(m_rootRect); gfxQ->setScissor(m_rootRect);
View::draw(gfxQ); View::draw(gfxQ);
m_splitView->draw(gfxQ); if (m_view)
m_view->draw(gfxQ);
gfxQ->resolveDisplay(m_renderTex); gfxQ->resolveDisplay(m_renderTex);
} }

View File

@ -0,0 +1,52 @@
#include "Specter/Translator.hpp"
#include <LogVisor/LogVisor.hpp>
namespace Specter
{
static LogVisor::LogModule Log("Specter::Translator");
Locale::Locale(const std::string& name, const std::string& fullName,
const unsigned char* yamlSource, size_t yamlLength)
: m_name(name), m_fullName(fullName)
{
Athena::io::YAMLDocReader reader;
yaml_parser_t parser;
yaml_parser_initialize(&parser);
yaml_parser_set_input_string(&parser, yamlSource, yamlLength);
reader.read(&parser);
m_rootNode = std::move(reader.releaseRootNode());
}
void Translator::setLocale(const Locale* targetLocale)
{
if (!targetLocale)
Log.report(LogVisor::FatalError, "null locale");
m_targetLocale = targetLocale;
}
static const std::string* RecursiveLookup(const Athena::io::YAMLNode& node,
std::string::const_iterator start,
std::string::const_iterator end)
{
for (std::string::const_iterator it = start ; it != end ; ++it)
{
if (*it == '/')
{
const Athena::io::YAMLNode* ch = node.findMapChild(std::string(start, it).c_str());
if (!ch)
return nullptr;
return RecursiveLookup(*ch, it+1, end);
}
}
const Athena::io::YAMLNode* ch = node.findMapChild(std::string(start, end).c_str());
if (!ch)
return nullptr;
return &ch->m_scalarString;
}
const std::string* Translator::translate(const std::string& key)
{
return RecursiveLookup(m_targetLocale->rootNode(), key.cbegin(), key.cend());
}
}