Skip to content

Commit

Permalink
simplify value loading
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloose committed Apr 29, 2024
1 parent 9af55b0 commit d95f016
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
6 changes: 5 additions & 1 deletion include/sorthem/ui/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <SFML/Graphics.hpp>
#include <string>
#include <sorthem/config.h>

namespace sorthem {

Expand All @@ -13,14 +14,17 @@ class Bar : public sf::Drawable {
sorthem::Bar::state m_state;
bool m_with_mark = false;
std::string m_mark;
bar_height_t m_value;
public:
inline static const sf::Color default_color = sf::Color::White;
inline static const sf::Color swapping_color = sf::Color(0xF33232FF); // red
inline static const sf::Color getting_color = sf::Color(0x9B54C3FF); // purple
inline static const sf::Color setting_color = sf::Color(0x2D43DBFF); // blue
inline static const sf::Color comparing_color = sf::Color(0xE79933FF); // orange

Bar();
Bar(bar_height_t value);

bar_height_t getValue();

void setSize(sf::Vector2f new_size);
sf::Vector2f getSize();
Expand Down
2 changes: 0 additions & 2 deletions include/sorthem/ui/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace sorthem {

class Graph : public sf::Drawable {
std::vector<Bar> m_bars;
// Initial data loaded on startup
std::vector<bar_height_t> m_initial_data;
// Points to the view of the window
const sf::View* m_win_view;
bar_height_t m_max_height;
Expand Down
7 changes: 6 additions & 1 deletion src/ui/bar.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sorthem/ui/bar.h>
#include <sorthem/config.h>

sorthem::Bar::Bar() : m_shape(), m_state(Bar::state::Iddle) {
sorthem::Bar::Bar(bar_height_t value) : m_shape(), m_state(Bar::state::Iddle), m_value(value) {
m_shape.setFillColor(Bar::default_color);
}

bar_height_t sorthem::Bar::getValue() {
return m_value;
}

void sorthem::Bar::setSize(sf::Vector2f new_size) {
m_shape.setSize(new_size);
}
Expand Down
20 changes: 8 additions & 12 deletions src/ui/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void sorthem::Graph::loadRectsValues() {
float win_height = m_win_view->getSize().y;
float rects_width = m_win_view->getSize().x / count;
for (std::size_t i = 0; i < count; i++) {
float height = win_height * m_initial_data[i] / m_max_height;
float height = win_height * m_bars[i].getValue() / m_max_height;
m_bars[i].setSize({ rects_width, height });
m_bars[i].setPosition({ i * rects_width, win_height - height });
}
Expand Down Expand Up @@ -209,27 +209,23 @@ void sorthem::Graph::loadArrayDataFromStdin() {
// print for debugging purposes
// std::cout << "data: " << str_arr << "\n";

bar_height_t max = -1;

while (iss >> str_num) {
try {
bar_height_t num = STR_TO_BAR_HEIGHT_T(str_num);
m_initial_data.push_back(num);
if (num > max) max = num;
m_bars.emplace_back(num);
}
catch (...) {
// TODO: handle exceptions better (with UI)
throw std::runtime_error("load: bad number: " + str_num + ".");
throw std::runtime_error("load: bad number: " + str_num);
}
}

/* Refresh the bars */

m_max_height = *std::max_element(
m_initial_data.begin(),
m_initial_data.end()
);
size_t count = m_initial_data.size();
// resize the vector to fit the new loaded data
m_bars.resize(count);
loadRectsValues();
m_max_height = max;
loadRectsValues(); // call once
}

/* Sorting operations */
Expand Down

0 comments on commit d95f016

Please sign in to comment.