Skip to content

Commit

Permalink
Add scaling in conf
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Dec 16, 2021
1 parent 3d04cd9 commit 6b85405
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/editor/GUI/item.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "item.hpp"

float GUI::Item::SCALE = 1.0f;
3 changes: 1 addition & 2 deletions include/editor/GUI/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct Observer

struct Item
{
static constexpr float SCALE = 2.0f;
// Attributes
sf::Vector2f size;
sf::Vector2f offset;
Expand Down Expand Up @@ -228,7 +227,7 @@ struct Item
void draw(sf::RenderTarget& target, const sf::Drawable& drawable, const sf::RenderStates& states)
{
sf::RenderStates default_states = states;
default_states.transform.scale(2.0f, 2.0f);
default_states.transform.scale(Conf::GUI_SCALE, Conf::GUI_SCALE);
default_states.transform.translate(offset);
target.draw(drawable, default_states);
}
Expand Down
8 changes: 4 additions & 4 deletions include/editor/GUI/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Scene
Scene(sf::RenderWindow& window_)
: window(window_)
, event_manager(window_, false)
, root(toVector2f(window_.getSize()) / Item::SCALE)
, root(toVector2f(window_.getSize()) / Conf::GUI_SCALE)
{
initializeEventsCallbacks();
}
Expand All @@ -49,7 +49,7 @@ struct Scene
{
const auto size = window.getSize();
const sf::Vector2f new_size{to<float>(size.x), to<float>(size.y)};
root.setSize(new_size / Item::SCALE);
root.setSize(new_size / Conf::GUI_SCALE);
window.setView(sf::View(new_size * 0.5f, new_size));
onSizeChange();
}
Expand All @@ -73,7 +73,7 @@ struct Scene

void dispatchClick(const sf::Event& e)
{
root.defaultOnClick(mouse_position / Item::SCALE, e.mouseButton.button);
root.defaultOnClick(mouse_position / Conf::GUI_SCALE, e.mouseButton.button);
}

void unclick(const sf::Event& e)
Expand Down Expand Up @@ -105,7 +105,7 @@ struct Scene

void mouseMove(int32_t x, int32_t y)
{
mouse_position = sf::Vector2f(to<float>(x), to<float>(y)) / Item::SCALE;
mouse_position = sf::Vector2f(to<float>(x), to<float>(y)) / Conf::GUI_SCALE;
root.defaultOnMouseMove(mouse_position);
}
};
Expand Down
4 changes: 2 additions & 2 deletions include/editor/editor_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ struct EditorScene : public GUI::Scene

void onSizeChange() override
{
renderer->setSize(root.size);
renderer->simulation.renderer.vp_handler.state.center = root.size;
renderer->size = root.size;
renderer->simulation.renderer.vp_handler.state.center = root.size * 0.5f * Conf::GUI_SCALE;
// This is to update mouse_position
simulation.renderer.vp_handler.wheelZoom(0);
}
Expand Down
4 changes: 2 additions & 2 deletions include/editor/world_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct WorldView : GUI::Item
{
if (button == sf::Mouse::Left) {
control_state.focus_requested = false;
simulation.renderer.vp_handler.click(relative_click_position * Item::SCALE);
simulation.renderer.vp_handler.click(relative_click_position * Conf::GUI_SCALE);
} else if (button == sf::Mouse::Right) {
action_button_click = true;
control_state.executeViewAction(simulation.renderer.vp_handler.getMouseWorldPosition());
Expand All @@ -68,7 +68,7 @@ struct WorldView : GUI::Item

void onMouseMove(sf::Vector2f new_mouse_position) override
{
simulation.renderer.vp_handler.setMousePosition(new_mouse_position * Item::SCALE);
simulation.renderer.vp_handler.setMousePosition(new_mouse_position * Conf::GUI_SCALE);
if (action_button_click) {
control_state.executeViewAction(simulation.renderer.vp_handler.getMouseWorldPosition());
}
Expand Down
7 changes: 7 additions & 0 deletions include/simulation/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct DefaultConf
static constexpr uint32_t MAX_COLONIES_COUNT = 4;
static sf::Color COLONY_COLORS[MAX_COLONIES_COUNT];
static uint32_t USE_FULLSCREEN;
static float GUI_SCALE;

static void loadTextures()
{
Expand Down Expand Up @@ -65,6 +66,9 @@ struct DefaultConf
DefaultConf<T>::USE_FULLSCREEN = std::atoi(line_c);
break;
case 3:
DefaultConf<T>::GUI_SCALE = std::atof(line_c);
break;
case 4:
DefaultConf<T>::ANTS_COUNT = std::atoi(line_c);
break;
default:
Expand Down Expand Up @@ -118,6 +122,9 @@ sf::Color DefaultConf<T>::COLONY_COLORS[MAX_COLONIES_COUNT] = {sf::Color::Red, s
template<typename T>
uint32_t DefaultConf<T>::USE_FULLSCREEN = 1;

template<typename T>
float DefaultConf<T>::GUI_SCALE = 1.0f;

using Conf = DefaultConf<int>;


Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ int main()
Simulation simulation(window);
// Create editor scene around it
GUI::Scene::Ptr scene = create<edtr::EditorScene>(window, simulation);
scene->resize();
// Main loop
while (window.isOpen()) {
// Update
Expand Down

0 comments on commit 6b85405

Please sign in to comment.