DrawLayerProxy, rudimentary preview in OSK

This commit is contained in:
zetaPRIME 2017-03-09 14:34:59 -05:00
parent 588817acc6
commit 92467e21c6
8 changed files with 135 additions and 11 deletions

View File

@ -2,6 +2,9 @@
#include "starlight/ThemeManager.h"
#include "starlight/InputManager.h"
#include "starlight/GFXManager.h"
#include "starlight/gfx/Font.h"
#include "starlight/ui/Image.h"
#include "starlight/ui/Button.h"
@ -12,12 +15,17 @@
using std::string;
using starlight::ThemeManager;
using starlight::InputManager;
using starlight::GFXManager;
using starlight::gfx::Font;
using starlight::ui::Image;
using starlight::ui::Button;
using starlight::ui::Label;
using starlight::ui::ScrollField;
using starlight::ui::DrawLayerProxy;
using starlight::ui::Form;
@ -38,6 +46,7 @@ OSK::OSK(osk::InputHandler* handler) : Form(true), handler(handler) {
auto actSym = [this](Button& key){
this->handler->InputSymbol(key.label);
this->OnKey();
};
Vector2 bs(24, 32);
@ -79,16 +88,19 @@ OSK::OSK(osk::InputHandler* handler) : Form(true), handler(handler) {
auto key = std::make_shared<Button>(VRect(bpen, bs));
key->rect.size.x *= 1.25;
key->SetText("< <");
key->eOnTap = [this](auto& btn){ this->handler->Backspace(); };
key->eOnTap = [this](auto& btn){ this->handler->Backspace(); this->OnKey(); };
touchScreen->Add(key);
// enter
bpen = bpstart + bs * Vector2(linestart[4] + 8, 4);
key = std::make_shared<Button>(VRect(bpen, bs));
key->rect.size.x *= 2.5;
key->SetText("Enter");
key->eOnTap = [this](auto& btn){ this->handler->Enter(); };
key->eOnTap = [this](auto& btn){ this->handler->Enter(); this->OnKey(); };
touchScreen->Add(key);
preview = std::make_shared<DrawLayerProxy>(VRect::touchScreen.TopEdge(68).Expand(-2), [this](auto& layer){ this->DrawPreview(layer); }, true);
touchScreen->Add(preview);
}
void OSK::Update(bool focused) {
@ -107,11 +119,13 @@ void OSK::Update(bool focused) {
}
}
void OSK::OnSymKey(const string& chr) {
//pText->append(chr);
//pText->append("sackboy ");
//auto& tx = *pText;
//ConfigManager::Get("user").Json()["log"].push_back(*pText);
//tx.assign("shickaxe");
//if (eOnModify) eOnModify();
void OSK::OnKey() {
preview->Refresh();
}
void OSK::DrawPreview(DrawLayerProxy& layer) {
if (true || handler->showPreview) {
static auto tc = ThemeManager::GetMetric<TextConfig>("/dialogs/OSK/preview");
tc.Print(layer.rect, handler->GetPreviewText(), Vector2::zero);
}
}

View File

@ -6,6 +6,7 @@
#include <memory>
#include "starlight/ui/Form.h"
#include "starlight/ui/DrawLayerProxy.h"
#include "starlight/dialog/osk/InputHandler.h"
@ -14,6 +15,7 @@ namespace starlight {
class OSK : public ui::Form, public ui::FormCreator<OSK> {
private:
std::shared_ptr<ui::UIContainer> setContainer;
std::shared_ptr<ui::DrawLayerProxy> preview;
public:
std::unique_ptr<osk::InputHandler> handler;
@ -23,7 +25,9 @@ namespace starlight {
void Update(bool focused) override;
void OnSymKey(const std::string& chr);
void OnKey();
void DrawPreview(ui::DrawLayerProxy& layer);
};
}
}

View File

@ -10,6 +10,11 @@ using starlight::dialog::osk::InputHandlerBuffered;
// DirectEdit
std::string& InputHandlerDirectEdit::GetPreviewText() { return *pText; }
unsigned int InputHandlerDirectEdit::GetCursor() { return pText->length(); }
void InputHandlerDirectEdit::SetCursor(unsigned int index) { }
void InputHandlerDirectEdit::InputSymbol(const string& sym) {
pText->append(sym);
if (eOnModify) eOnModify();
@ -34,6 +39,11 @@ void InputHandlerDirectEdit::Done() {
// Buffered
std::string& InputHandlerBuffered::GetPreviewText() { return buffer; }
unsigned int InputHandlerBuffered::GetCursor() { return buffer.length(); }
void InputHandlerBuffered::SetCursor(unsigned int index) { }
void InputHandlerBuffered::InputSymbol(const string& sym) {
buffer.append(sym);
}

View File

@ -21,6 +21,11 @@ namespace starlight {
InputHandler() = default;
virtual ~InputHandler() = default;
virtual std::string& GetPreviewText() { static std::string p = ""; return p; }
virtual unsigned int GetCursor() { return 0; }
virtual void SetCursor(unsigned int index) { }
virtual void InputSymbol(const std::string& sym) { }
virtual void Backspace() { }
virtual void Enter() { }
@ -42,6 +47,11 @@ namespace starlight {
: multiLine(multiLine), pText(textptr), minIndex(minIndex), eOnModify(onModify), eOnFinalize(onFinalize) { }
~InputHandlerDirectEdit() override { }
std::string& GetPreviewText() override;
unsigned int GetCursor() override;
void SetCursor(unsigned int index) override;
void InputSymbol(const std::string& sym) override;
void Backspace() override;
void Enter() override;
@ -57,9 +67,14 @@ namespace starlight {
std::function<void(const std::string&)> eOnFinalize = { };
InputHandlerBuffered(const std::string& text = "", bool multiLine = false, std::function<void(const std::string&)> onFinalize = {})
: showPreview(true), buffer(text), multiLine(multiLine), eOnFinalize(onFinalize) { }
: buffer(text), multiLine(multiLine), eOnFinalize(onFinalize) { this->showPreview = true; }
~InputHandlerBuffered() override { }
std::string& GetPreviewText() override;
unsigned int GetCursor() override;
void SetCursor(unsigned int index) override;
void InputSymbol(const std::string& sym) override;
void Backspace() override;
void Enter() override;

View File

@ -0,0 +1,33 @@
#include "DrawLayerProxy.h"
#include "starlight/GFXManager.h"
using starlight::GFXManager;
using starlight::gfx::DrawContextCanvas;
using starlight::ui::DrawLayerProxy;
void DrawLayerProxy::Refresh() {
canvas.reset();
MarkForRedraw();
}
void DrawLayerProxy::PreDraw() {
if (useCanvas) {
if (!canvas) {
canvas = std::make_unique<DrawContextCanvas>(rect.size);
canvas->Clear();
GFXManager::PushContext(canvas.get());
if (eDraw) eDraw(*this);
GFXManager::PopContext();
}
} else canvas.reset();
}
void DrawLayerProxy::Draw() {
if (canvas) {
canvas->Draw(VRect(rect.pos, canvas->rect.size));
} else {
if (eDraw) eDraw(*this);
}
}

View File

@ -0,0 +1,40 @@
#pragma once
#include "starlight/_global.h"
#include <memory>
#include <functional>
#include "starlight/datatypes/Vector2.h"
#include "starlight/datatypes/VRect.h"
#include "starlight/gfx/DrawContextCanvas.h"
#include "starlight/ui/UIElement.h"
namespace starlight {
namespace ui {
class DrawLayerProxy : public UIElement {
private:
protected:
public:
bool useCanvas = false;
std::unique_ptr<gfx::DrawContextCanvas> canvas;
std::function<void(DrawLayerProxy&)> eDraw;
DrawLayerProxy(VRect rect, std::function<void(DrawLayerProxy&)> drawFunc, bool useCanvas = false) : useCanvas(useCanvas), eDraw(drawFunc) { this->rect = rect; }
~DrawLayerProxy() override { }
void Refresh();
//void Update() override;
void PreDraw() override;
void Draw() override;
//
};
}
}

View File

@ -11,6 +11,7 @@ roadmap to first release, in no particular order {
InputManager::OpenKeyboard
}
textbox widget
- "draw proxy" UIElement (takes a std::function for draw operation)
add license!! (MIT?)
ADD README.MD PLS

View File

@ -32,6 +32,13 @@
"dialogs" : {
"messageBox" : {
"size" : [240, 160]
},
"OSK" : {
"preview" : {
"font" : "default.16",
"textColor" : "white",
"borderColor" : "black"
}
}
},