From 905d2a7761f0e8f2b8d9bebb75bfb9638cdfc013 Mon Sep 17 00:00:00 2001 From: Adspartan Date: Fri, 22 Nov 2024 18:02:49 +0100 Subject: [PATCH] noggit: ui: texture_swapper: add hardness and pressure parameters for the brush mode --- src/noggit/MapChunk.cpp | 4 ++-- src/noggit/MapChunk.h | 2 +- src/noggit/World.cpp | 6 +++--- src/noggit/World.h | 2 +- src/noggit/texture_set.cpp | 10 +++++++--- src/noggit/texture_set.hpp | 3 ++- src/noggit/ui/texture_swapper.cpp | 4 ++++ src/noggit/ui/texture_swapper.hpp | 16 ++++++++++------ src/noggit/ui/texturing_tool.cpp | 9 ++++++--- 9 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/noggit/MapChunk.cpp b/src/noggit/MapChunk.cpp index 5c20e4895..9e66908e6 100644 --- a/src/noggit/MapChunk.cpp +++ b/src/noggit/MapChunk.cpp @@ -1473,10 +1473,10 @@ bool MapChunk::paintTexture(math::vector_3d const& pos, Brush* brush, float stre return texture_set->paintTexture(xbase, zbase, pos.x, pos.z, brush, strength, pressure, std::move (texture)); } -bool MapChunk::replaceTexture(math::vector_3d const& pos, float radius, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture) +bool MapChunk::replaceTexture(math::vector_3d const& pos, Brush const& brush, float change, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture) { texture_set_changed(); - return texture_set->replace_texture(xbase, zbase, pos.x, pos.z, radius, old_texture, std::move (new_texture)); + return texture_set->replace_texture(xbase, zbase, pos.x, pos.z, brush, change, old_texture, std::move (new_texture)); } bool MapChunk::canPaintTexture(std::string const& texture) diff --git a/src/noggit/MapChunk.h b/src/noggit/MapChunk.h index 4bdf444a4..2bde85a83 100644 --- a/src/noggit/MapChunk.h +++ b/src/noggit/MapChunk.h @@ -199,7 +199,7 @@ class MapChunk //! \todo implement Action stack for these bool paintTexture(math::vector_3d const& pos, Brush *brush, float strength, float pressure, scoped_blp_texture_reference texture); - bool replaceTexture(math::vector_3d const& pos, float radius, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture); + bool replaceTexture(math::vector_3d const& pos, Brush const& brush, float change, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture); bool canPaintTexture(std::string const& texture); int addTexture(scoped_blp_texture_reference texture); void switchTexture(scoped_blp_texture_reference const& oldTexture, scoped_blp_texture_reference newTexture); diff --git a/src/noggit/World.cpp b/src/noggit/World.cpp index e8bd633a5..235ebdab7 100644 --- a/src/noggit/World.cpp +++ b/src/noggit/World.cpp @@ -2052,13 +2052,13 @@ bool World::sprayTexture(math::vector_3d const& pos, Brush *brush, float strengt return succ; } -bool World::replaceTexture(math::vector_3d const& pos, float radius, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture) +bool World::replaceTexture(math::vector_3d const& pos, Brush const& brush, float change, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture) { return for_all_chunks_in_range - ( pos, radius + ( pos, brush.get_radius() , [&](MapChunk* chunk) { - return chunk->replaceTexture(pos, radius, old_texture, new_texture); + return chunk->replaceTexture(pos, brush, change, old_texture, new_texture); } ); } diff --git a/src/noggit/World.h b/src/noggit/World.h index 59d46aa9b..aa3a92bee 100644 --- a/src/noggit/World.h +++ b/src/noggit/World.h @@ -263,7 +263,7 @@ class World void blurTerrain(math::vector_3d const& pos, float remain, float radius, int BrushType, flatten_mode const& mode); bool paintTexture(math::vector_3d const& pos, Brush *brush, float strength, float pressure, scoped_blp_texture_reference texture); bool sprayTexture(math::vector_3d const& pos, Brush *brush, float strength, float pressure, float spraySize, float sprayPressure, scoped_blp_texture_reference texture); - bool replaceTexture(math::vector_3d const& pos, float radius, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture); + bool replaceTexture(math::vector_3d const& pos, Brush const& brush, float change, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture); void clear_on_chunks ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures , bool textures_below_threshold, float alpha_threshold, bool texture_flags, bool liquids diff --git a/src/noggit/texture_set.cpp b/src/noggit/texture_set.cpp index bf3393cbc..642466ca0 100644 --- a/src/noggit/texture_set.cpp +++ b/src/noggit/texture_set.cpp @@ -660,12 +660,15 @@ bool TextureSet::replace_texture( float xbase , float zbase , float x , float z - , float radius + , Brush const& brush + , float change , scoped_blp_texture_reference const& texture_to_replace , scoped_blp_texture_reference replacement_texture ) { float dist = misc::getShortestDist(x, z, xbase, zbase, CHUNKSIZE); + float radius = brush.get_radius(); + float inner_radius = brush.get_inner_radius(); if (dist > radius) { @@ -724,8 +727,9 @@ bool TextureSet::replace_texture( float xbase { int offset = j * 64 + i; - amap[new_tex_level][offset] += amap[old_tex_level][offset]; - amap[old_tex_level][offset] = 0.f; + float v = amap[old_tex_level][offset] * brush.value_at_dist(dist) * change; + amap[new_tex_level][offset] += v; + amap[old_tex_level][offset] -= v; changed = true; } diff --git a/src/noggit/texture_set.hpp b/src/noggit/texture_set.hpp index 39292a69e..6def32e77 100644 --- a/src/noggit/texture_set.hpp +++ b/src/noggit/texture_set.hpp @@ -56,7 +56,8 @@ class TextureSet , float zbase , float x , float z - , float radius + , Brush const& brush + , float change , scoped_blp_texture_reference const& texture_to_replace , scoped_blp_texture_reference replacement_texture ); diff --git a/src/noggit/ui/texture_swapper.cpp b/src/noggit/ui/texture_swapper.cpp index 47fa6b480..772f7bc8a 100644 --- a/src/noggit/ui/texture_swapper.cpp +++ b/src/noggit/ui/texture_swapper.cpp @@ -25,6 +25,8 @@ namespace noggit : QWidget (parent) , _texture_to_swap() , _radius(15.f) + , _hardness(0.5f) + , _pressure(0.75f) { setWindowTitle ("Swap"); setWindowFlags (Qt::Tool | Qt::WindowStaysOnTopHint); @@ -51,6 +53,8 @@ namespace noggit _brush_mode_group->setLayout(brush_layout); brush_layout->addRow(new slider_spinbox("Radius", &_radius, 0.f, 100.f, 2, brush_content)); + brush_layout->addRow(new slider_spinbox("Hardness", &_hardness, 0.f, 1.f, 2, brush_content)); + brush_layout->addRow(new slider_spinbox("Pressure", &_pressure, 0.f, 1.f, 2, brush_content)); connect(select, &QPushButton::clicked, [&]() { _texture_to_swap = selected_texture::get(); diff --git a/src/noggit/ui/texture_swapper.hpp b/src/noggit/ui/texture_swapper.hpp index 723e6d4e2..13e4107fe 100644 --- a/src/noggit/ui/texture_swapper.hpp +++ b/src/noggit/ui/texture_swapper.hpp @@ -3,6 +3,7 @@ #pragma once #include +#include #include #include #include @@ -33,18 +34,21 @@ namespace noggit return _texture_to_swap; } - float radius() const - { - return _radius.get(); - } + float radius() const { return _radius.get(); } + float hardness() const { return _hardness.get(); } + float pressure() const { return _pressure.get(); } void change_radius(float change) { _radius.change(change); } + void change_hardness(float change) { _hardness.change(change); } + void change_pressure(float change) { _pressure.change(change); } bool brush_mode() const { return _brush_mode_group->isChecked(); } + Brush brush() const { return Brush(radius(), hardness()); } + void toggle_brush_mode() { _brush_mode_group->setChecked(!_brush_mode_group->isChecked()); @@ -57,13 +61,13 @@ namespace noggit private: boost::optional _texture_to_swap; float_property _radius; + float_property _hardness; + float_property _pressure; private: current_texture* _texture_to_swap_display; QGroupBox* _brush_mode_group; - QSlider* _radius_slider; - QDoubleSpinBox* _radius_spin; }; } } diff --git a/src/noggit/ui/texturing_tool.cpp b/src/noggit/ui/texturing_tool.cpp index 0f24e2675..ac4a38027 100644 --- a/src/noggit/ui/texturing_tool.cpp +++ b/src/noggit/ui/texturing_tool.cpp @@ -375,6 +375,7 @@ namespace noggit switch (_texturing_mode) { case texturing_mode::paint: return _hardness.get(); + case texturing_mode::swap: return _texture_switcher->brush_mode() ? _texture_switcher->hardness() : 0.f; default: return 0.f; } } @@ -386,16 +387,16 @@ namespace noggit void texturing_tool::paint (World* world, math::vector_3d const& pos, float dt, scoped_blp_texture_reference texture) { - float strength = 1.0f - pow(1.0f - _pressure.get(), dt * 10.0f); - if (_texturing_mode == texturing_mode::swap) { + float strength = 1.0f - pow(1.0f - _texture_switcher->pressure(), dt * 10.0f); + auto to_swap (_texture_switcher->texture_to_swap()); if (to_swap) { if (_texture_switcher->brush_mode()) { - world->replaceTexture(pos, _texture_switcher->radius(), to_swap.get(), texture); + world->replaceTexture(pos, _texture_switcher->brush(), strength, to_swap.get(), texture); } else { @@ -405,6 +406,8 @@ namespace noggit } else if (_texturing_mode == texturing_mode::paint) { + float strength = 1.0f - pow(1.0f - _pressure.get(), dt * 10.0f); + if (_spray_mode_group->isChecked()) { world->sprayTexture(pos, &_spray_brush, alpha_target(), strength, _radius.get(), _spray_pressure.get(), texture);