Browse Source

scheduler/cursor work, sync inputText with page binding

pull/2/head
Odilitime 5 years ago
parent
commit
a5c70cd844
  1. 110
      src/graphics/components/InputComponent.cpp
  2. 5
      src/graphics/components/InputComponent.h

110
src/graphics/components/InputComponent.cpp

@ -1,14 +1,15 @@ @@ -1,14 +1,15 @@
#include "InputComponent.h"
#include <iostream>
#include "../opengl/Window.h"
#include "../text/TextRasterizerCache.h"
#include "../../scheduler.h"
extern TextRasterizerCache *rasterizerCache;
extern std::unique_ptr<Scheduler> scheduler;
// : BoxComponent(rawX, rawY, rawWidth, rawHeight, passedWindowWidth, passedWindowHeight)
InputComponent::InputComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight) {
//std::cout << "InputComponent::InputComponent - data" << std::endl;
//std::cout << "InputComponent::InputComponent - create, boundToPage" << boundToPage << std::endl;
//std::cout << "InputComponent::InputComponent - window: " << windowWidth << "x" << windowHeight << " passed " << passedWindowWidth << "x" << passedWindowHeight << std::endl;
//boundToPage = true;
@ -17,12 +18,41 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r @@ -17,12 +18,41 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r
// set up state
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
// lets not set, like resize set, and then maybe parent width will be correct
// didn't really changed anything
width = rawWidth;
height = rawHeight;
// ugh
x = rawX;
y = rawY;
lastRenderedWindowHeight = windowHeight;
// const float rawX, const float rawY, const float rawWidth, const float rawHeight, const unsigned int hexColor, const int passedWindowWidth, const int passedWindowHeight
this->cursorBox = new BoxComponent(x, y, 2, rawHeight, 0x000000FF, windowWidth, windowHeight);
this->cursorBox->boundToPage = this->boundToPage;
this->cursorBox->x = x;
this->cursorBox->y = y;
if (boundToPage) {
this->cursorBox->y = this->windowHeight + y - 13;
}
this->cursorBox->resize(this->windowWidth, this->windowHeight);
this->updateText();
onFocus=[this]() {
this->focused = true;
this->cursorBox->x = x ;
this->cursorBox->y = y;
if (this->boundToPage) {
this->cursorBox->y = this->windowHeight + y - 13;
}
this->cursorBox->resize(this->windowWidth, this->windowHeight);
this->win->renderDirty = true;
};
onBlur=[this]() {
this->focused = false;
this->win->renderDirty = true;
};
//std::cout << "InputComponent::InputComponent - placing box at " << (int)x << "," << (int)y << " size: " << (int)width << "x" << (int)height << std::endl;
@ -90,7 +120,8 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r @@ -90,7 +120,8 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r
}
void InputComponent::render() {
//std::cout << "InputComponent::render" << std::endl;
//std::cout << "InputComponent::render - at " << (int)x << "," << (int)y << std::endl;
//std::cout << "InputComponent::render - boundToPage " << boundToPage << std::endl;
GLenum glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "InputComponent::render - start not ok: " << glErr << std::endl;
@ -135,6 +166,7 @@ void InputComponent::render() { @@ -135,6 +166,7 @@ void InputComponent::render() {
if(glErr != GL_NO_ERROR) {
std::cout << "InputComponent::render - glGetUniformLocation not ok: " << glErr << std::endl;
}
// it's about the document transformMatrix
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, window->transformMatrix);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
@ -145,10 +177,26 @@ void InputComponent::render() { @@ -145,10 +177,26 @@ void InputComponent::render() {
userInputText->render();
glUseProgram(window->textureProgram);
}
if (focused) {
//std::cout << "Rendering cursor" << std::endl;
// blink cursor
if (cursorTimer != nullptr) {
scheduler->clearInterval(cursorTimer);
}
cursorTimer = scheduler->setInterval([this]() {
this->showCursor = !this->showCursor;
//std::cout << "showCursor " << this->showCursor << std::endl;
this->win->renderDirty = true;
}, 500);
// render it if we need to
if (showCursor) {
cursorBox->render();
}
}
}
// this doesn't seem to work
// text is fine though
void InputComponent::resize(const int passedWindowWidth, const int passedWindowHeight) {
//std::cout << "InputComponent::resize" << std::endl;
//std::cout << "InputComponent::resize - rasterizing at " << (int)x << "x" << (int)y << " size: " << (int)width << "x" << (int)height << std::endl;
@ -157,7 +205,9 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH @@ -157,7 +205,9 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH
// set up state
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
//std::cout << "InputComponent::resize - boxShader: " << useBoxShader << " boundToPage: " << boundToPage << std::endl;
/*
if (!boundToPage) {
// ok because box shader is anchored to the bottom of the screen
@ -184,25 +234,11 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH @@ -184,25 +234,11 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH
float vx = x;
float vy = y;
//float vx1 = x + width;
//float vy1 = y - height;
/*
std::cout << "placing box at " << (int)vx << "," << (int)vy << " size: " << (int)width << "x" << (int)height << " v1: " << (int)vx1 << "," << (int)vy1 << std::endl;
//float vWidth = width;
//float vHeight = height;
boundToPage = true;
pointToViewport(vx, vy);
pointToViewport(vx1, vy1);
std::cout << "TRUE placing box at GL v: " << vx << "," << vy << " v1: " << vx1 << "," << vy1 << std::endl;
vx = x;
vy = y;
vx1 = x + width;
vy1 = y - height;
boundToPage = false;
*/
if (boundToPage) {
vy = this->windowHeight + y - height;
//std::cout << "InputComponent::resize - Adjust y to " << vy << " from " << y << " h: " << (int)height << std::endl;
}
pointToViewport(vx, vy);
//pointToViewport(vx1, vy1);
float vWidth = width;
float vHeight = height;
@ -210,15 +246,8 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH @@ -210,15 +246,8 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH
float vx1 = vx + vWidth;
float vy1 = vy + vHeight;
//std::cout << "FALSE placing box at GL v: " << vx << "," << vy << " v1: " << vx1 << "," << vy1 << std::endl;
//std::cout << "InputComponent::resize - placing box at GL " << vx << "," << vy << " to " << vx1 << "," << vy1 << " size: " << vWidth << "x" << vHeight << std::endl;
// converts 512 to 1 and 1 to 2
//std::cout << "vWidth before: " << (int)vWidth << std::endl;
//distanceToViewport(vWidth, vHeight);
//std::cout << "vWidth after: " << (int)vWidth << std::endl;
vertices[(0 * 5) + 0] = vx;
vertices[(0 * 5) + 1] = vy1;
@ -232,19 +261,15 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH @@ -232,19 +261,15 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH
vertices[(3 * 5) + 1] = vy;
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0); // protect what we created against any further modification
//updateText();
if (userInputText) {
// do we need updateText here?
userInputText->resize(passedWindowWidth, passedWindowHeight);
}
}
@ -273,6 +298,7 @@ void InputComponent::updateText() { @@ -273,6 +298,7 @@ void InputComponent::updateText() {
} else {
userInputText->y = y - windowHeight + 16;
}
userInputText->boundToPage = this->boundToPage;
//std::cout << "placed userInputText at " << static_cast<int>(x) << "," << static_cast<int>(y - windowHeight) << std::endl;
// 125 pixels width
// but first we need to know how wide the text is
@ -299,6 +325,16 @@ void InputComponent::updateText() { @@ -299,6 +325,16 @@ void InputComponent::updateText() {
//std::cout << "scrolling text" << std::endl;
userInputText->rasterStartX = estWidth - width;
}
// this is texture shader coordinates now (not text shader coords)
cursorBox->x = x + estWidth;
if (boundToPage) {
cursorBox->y = windowHeight + y - 13;
} else {
cursorBox->y = y;
}
//std::cout << "placing cursor at " << (int)cursorBox->x << "," << (int)cursorBox->y << std::endl;
cursorBox->resize(windowWidth, windowHeight);
userInputText->noWrap = true;
// why does changing the width mess shit up?
//std::cout << "InputComponent::updateText - our width: " << static_cast<int>(width) << " windowWidth: " << windowWidth << std::endl;

5
src/graphics/components/InputComponent.h

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
#include "BoxComponent.h"
#include "TextComponent.h"
#include "../opengl/Window.h"
#include "../../scheduler.h"
class Window;
@ -22,9 +23,13 @@ public: @@ -22,9 +23,13 @@ public:
void updateText();
std::string value="";
TextComponent *userInputText = nullptr;
BoxComponent *cursorBox = nullptr;
std::function<void(std::string value)> onEnter = nullptr;
// needed for our shader's resizing
int lastRenderedWindowHeight;
bool focused = false;
bool showCursor = true;
std::shared_ptr<timer_handle> cursorTimer = nullptr;
// handle to signal that a redraw is needed, and access to shader programs
Window *win;
};

Loading…
Cancel
Save