Improved visibility of brush, eraser, and selection tools

This commit is contained in:
Yousef Khadadeh 2026-03-20 16:24:52 +03:00
parent 15e98c6e1f
commit 10ee12254c
5 changed files with 45 additions and 12 deletions

View file

@ -7,6 +7,32 @@
using namespace NetworkingObjects;
const char* visibleBlendModeCode = R"V(
vec4 main(vec4 src, vec4 dst) {
float value = max(max(dst.x, dst.y), dst.z) / dst.a;
vec4 actualSrc;
if(value > 0.6)
actualSrc = vec4(vec3(0.0), src.a);
else
actualSrc = vec4(src.a);
return actualSrc + (1.0 - actualSrc.a) * dst;
}
)V";
sk_sp<SkBlender> CanvasTheme::get_visible_blend_mode() {
static sk_sp<SkBlender> blender = nullptr;
if(!blender) {
auto effect = SkRuntimeEffect::MakeForBlender(SkString(visibleBlendModeCode));
if(!effect.effect) {
std::cout << "[CanvasTheme::get_visible_blend_mode] " << " blender construction error\n";
std::cout << effect.errorText.c_str() << std::endl;
throw std::runtime_error("Shader Compile Failure");
}
blender = effect.effect->makeBlender(nullptr);
}
return blender;
}
CanvasTheme::CanvasTheme(World& w):
world(w)
{}
@ -37,13 +63,11 @@ void CanvasTheme::set_back_color(const Vector3f& newBackColor) {
void CanvasTheme::set_tool_front_color(DrawingProgram& drawP) {
Vector3f newHSV = rgb_to_hsv<Vector3f>(backColor->c);
SkColor4f oldToolFrontColor = toolFrontColor;
if(newHSV.z() >= 0.6f)
toolFrontColor = SkColor4f{0.0f, 0.0f, 0.0f, 1.0f};
else
toolFrontColor = SkColor4f{1.0f, 1.0f, 1.0f, 1.0f};
if(oldToolFrontColor != toolFrontColor)
drawP.drawCache.clear_own_cached_surfaces();
drawP.drawCache.clear_own_cached_surfaces();
}
void CanvasTheme::register_class() {

View file

@ -6,6 +6,8 @@
#include <Helpers/Serializers.hpp>
#include <Helpers/VersionNumber.hpp>
#include <include/effects/SkRuntimeEffect.h>
class DrawingProgram;
class World;
@ -21,6 +23,9 @@ class CanvasTheme {
void write_create_message(cereal::PortableBinaryOutputArchive& a) const;
void save_file(cereal::PortableBinaryOutputArchive& a) const;
void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version);
static sk_sp<SkBlender> get_visible_blend_mode();
private:
void set_tool_front_color(DrawingProgram& drawP);
struct BackColor {

View file

@ -522,6 +522,7 @@ SkPaint DrawingProgram::select_tool_line_paint() {
selectLinePaint.setStrokeWidth(3);
selectLinePaint.setColor4f(world.canvasTheme.get_tool_front_color());
selectLinePaint.setPathEffect(lassoLineDashEffect);
selectLinePaint.setBlender(CanvasTheme::get_visible_blend_mode());
return selectLinePaint;
}

View file

@ -187,17 +187,21 @@ void BrushTool::draw(SkCanvas* canvas, const DrawData& drawData) {
if(!drawP.world.main.input.isTouchDevice && drawData.main->world->drawProg.controls.cursorHoveringOverCanvas) {
auto relativeWidthResult = drawP.world.main.toolConfig.get_relative_width_stroke_size(drawP, drawP.world.drawData.cam.c.inverseScale);
if(relativeWidthResult.first.has_value()) {
SkPaint linePaint;
linePaint.setColor4f(drawP.world.canvasTheme.get_tool_front_color());
linePaint.setStroke(true);
linePaint.setStrokeWidth(0.0f);
float width = relativeWidthResult.first.value();
if(objInfoBeingEdited)
width *= penWidth * 0.5f;
else
width *= 0.5f;
width += 1.0f;
Vector2f pos = drawData.main->input.mouse.pos;
canvas->drawCircle(pos.x(), pos.y(), width, linePaint);
SkPaint linePaint;
linePaint.setColor4f({1.0f, 1.0f, 1.0f, 1.0f});
linePaint.setStyle(SkPaint::kStroke_Style);
linePaint.setStrokeCap(SkPaint::kRound_Cap);
linePaint.setStrokeWidth(0.0f);
linePaint.setBlender(CanvasTheme::get_visible_blend_mode());
SkPath circ = SkPath::Circle(pos.x(), pos.y(), width);
canvas->drawPath(circ, linePaint);
}
}
}

View file

@ -2,6 +2,7 @@
#include "../DrawingProgram.hpp"
#include "../../MainProgram.hpp"
#include "../../DrawData.hpp"
#include <include/core/SkBlendMode.h>
#include <include/core/SkPathBuilder.h>
EraserTool::EraserTool(DrawingProgram& initDrawP):
@ -118,14 +119,12 @@ void EraserTool::draw(SkCanvas* canvas, const DrawData& drawData) {
auto relativeWidthResult = drawP.world.main.toolConfig.get_relative_width_stroke_size(drawP, drawP.world.drawData.cam.c.inverseScale);
if(relativeWidthResult.first.has_value()) {
float width = relativeWidthResult.first.value() * 0.5f;
SkColor4f c = drawP.world.canvasTheme.get_tool_front_color();
c.fA = 0.5f;
SkPaint linePaint;
linePaint.setColor4f(c);
linePaint.setColor4f({1.0f, 1.0f, 1.0f, 0.5f});
linePaint.setStyle(SkPaint::kStroke_Style);
linePaint.setStrokeCap(SkPaint::kRound_Cap);
linePaint.setStrokeWidth(width * 2.0f);
linePaint.setBlender(CanvasTheme::get_visible_blend_mode());
SkPathBuilder erasePath;
erasePath.moveTo(convert_vec2<SkPoint>(lastPos));
erasePath.lineTo(convert_vec2<SkPoint>(drawData.main->input.mouse.pos));