Giter Club home page Giter Club logo

Comments (2)

raygit83 avatar raygit83 commented on September 18, 2024

So, here's am minimal code example that implements an eyedropper that shows the pixel color under the mouse cursor. The code works fine unless we embed that view in a CLayeredViewContainer on Mac, pa->getColor(color); always returns black in that case. On Windows it works either way which implies that it has something to do with the CALayer based implementation.

I'm aware that the backround graphic could be accessed directly, but imagine the CEyedropperView ::drawRect() implementation could draw anything.

class CEyedropperView : public CView
{
public:
	CEyedropperView(const CRect &size, CFrame *parent, CBitmap *hBackround) : CView(size)
	{
		setBackground(hBackround);
		oc = COffscreenContext::create(parent, getWidth(), getHeight());
	}

	virtual void drawRect(CDrawContext *pContext, const CRect& updateRect) override
	{
		// Draw contents to COffscreenContext.
		if(oc)
		{
			oc->beginDraw();
			CView::drawRect(oc, updateRect);
			oc->endDraw();
		}

		// NOTE: We could simply use oc->copyFrom() here so stuff doesn't need
		// to be drawn twice, but that would also require oc to be oversampled
		// for HighDPI / Retina displays. That doesn't have anything to do with
		// the issue itself though
		CView::drawRect(pContext, updateRect);

		// Draw eyedropper: The oc's bitmap is used to retrieve the pixel values
		// under the mouse cursor
		if(oc && eyedropperActive)
		{
			auto pa = VSTGUI::owned(CBitmapPixelAccess::create(oc->getBitmap()));
			if(pa)
			{
				CColor color;

				pa->setPosition(uint32_t(lastMousePoint.x), uint32_t(lastMousePoint.y));
				pa->getColor(color);

				CRect rect(-15.0, -15.0, +15.0, +15.0);
				rect.offset(lastMousePoint);

				pContext->setDrawMode(kAntiAliasing);
				pContext->setLineWidth(2.0);
				pContext->setFrameColor(kWhiteCColor);
				pContext->setFillColor(color);
				pContext->drawEllipse(rect, kDrawFilledAndStroked);
			}
		}
	}

	// Keep track of mouse cursor position
	virtual CMouseEventResult onMouseMoved(CPoint& where, const CButtonState& buttons) override
	{
		if(where != lastMousePoint)
		{
			lastMousePoint = where;
			invalid();
		}
		return CView::onMouseMoved(where, buttons);
	}

	virtual CMouseEventResult onMouseEntered(CPoint& where, const CButtonState& buttons) override
	{
		if(getFrame())
			getFrame()->setCursor(kCursorHand);

		eyedropperActive = true;
		invalid();
		return kMouseEventHandled;
	}

	virtual CMouseEventResult onMouseExited(CPoint& where, const CButtonState& buttons) override
	{
		if(getFrame())
			getFrame()->setCursor(kCursorDefault);

		eyedropperActive = false;
		invalid();
		return kMouseEventHandled;
	}

	CLASS_METHODS(CEyedropperView, CView);

protected:
	SharedPointer<COffscreenContext> oc;

	CPoint lastMousePoint;
	bool eyedropperActive{false};
};

from vstgui.

scheffle avatar scheffle commented on September 18, 2024

OK, now I understand the issue. The problem is, that CLayeredViewContainers are an optimization and are not redrawn in a normal way. So you cannot call draw(..) or drawRect(..) on it. That are no-ops.
A possible workaround is, to find all instances in your view hierarchy and after you've drawn the normal views, you call drawViewLayer() on that instances (you have to cast to IPlatformViewLayerDelegate before, otherwise the method is protected). You may have to offset the draw context by the top-left position of the view relative to the frame.

from vstgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.