Plotter element
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#include "Plotter.hpp"
|
||||
|
||||
#include "../../core/Batch2D.h"
|
||||
#include "../../core/Font.h"
|
||||
#include "../../core/GfxContext.h"
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../../util/stringutil.h"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
void Plotter::act(float delta) {
|
||||
index = index + 1 % dmwidth;
|
||||
int value = static_cast<int>(delta * multiplier);
|
||||
points[index % dmwidth] = std::min(value, dmheight);
|
||||
}
|
||||
|
||||
void Plotter::draw(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->lineWidth(1);
|
||||
for (int i = index+1; i < index+dmwidth; i++) {
|
||||
int j = i % dmwidth;
|
||||
batch->line(
|
||||
pos.x + i - index, pos.y + size.y - points[j],
|
||||
pos.x + i - index, pos.y + size.y, 1.0f, 1.0f, 1.0f, 0.2f
|
||||
);
|
||||
}
|
||||
batch->setColor({1,1,1,0.2f});
|
||||
batch->lineRect(pos.x, pos.y, dmwidth, dmheight);
|
||||
for (int y = 0; y < dmheight; y += 16) {
|
||||
batch->line(
|
||||
pos.x+dmwidth-4, pos.y+dmheight-y,
|
||||
pos.x+dmwidth+4, pos.y+dmheight-y,
|
||||
1.0f, 1.0f, 1.0f, 0.2f
|
||||
);
|
||||
}
|
||||
|
||||
int current_point = static_cast<int>(points[index % dmwidth]);
|
||||
auto font = assets->getFont("normal");
|
||||
for (int y = 0; y < dmheight; y += labelsInterval) {
|
||||
std::wstring string;
|
||||
if (current_point/16 == y/labelsInterval) {
|
||||
batch->setColor({1,1,1,0.5f});
|
||||
string = util::to_wstring(current_point / multiplier, 3);
|
||||
} else {
|
||||
batch->setColor({1,1,1,0.2f});
|
||||
string = util::to_wstring(y / multiplier, 3);
|
||||
}
|
||||
font->draw(batch, string, pos.x+dmwidth+2, pos.y+dmheight-y-labelsInterval);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
|
||||
#define GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
|
||||
|
||||
#include "UINode.h"
|
||||
#include "../../../typedefs.h"
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Assets;
|
||||
class GfxContext;
|
||||
|
||||
namespace gui {
|
||||
class Plotter : public gui::UINode {
|
||||
std::unique_ptr<int[]> points;
|
||||
float multiplier;
|
||||
int index = 0;
|
||||
int dmwidth;
|
||||
int dmheight;
|
||||
int labelsInterval;
|
||||
public:
|
||||
Plotter(uint width, uint height, float multiplier, int labelsInterval)
|
||||
: gui::UINode(glm::vec2(width, height)),
|
||||
multiplier(multiplier),
|
||||
dmwidth(width-50),
|
||||
dmheight(height),
|
||||
labelsInterval(labelsInterval)
|
||||
{
|
||||
points = std::make_unique<int[]>(dmwidth);
|
||||
}
|
||||
|
||||
void act(float delta) override;
|
||||
void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_CONTROLS_H_
|
||||
#define GRAPHICS_UI_ELEMENTS_CONTROLS_H_
|
||||
|
||||
// TODO: move elements to different files
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
Reference in New Issue
Block a user