Skip to content

Button to add theWinName parameter #102

@chen-zili

Description

@chen-zili

I now need to create two Windows, adding a button to each window.

		if (cvui::button(Mat1, 100, 100, 40, 20, "button1"))
		{
			cout << "button1" << endl;
		}


		if (cvui::button(Mat2, 100, 100, 40, 20, "button2"))
		{
			cout << "button2" << endl;
		}

In practice, however, the button is only bound to the latter, because the Button function does not specify a window name.

So, I modify the button function


	bool button(cv::String winName, cv::Mat& theWhere, int theX, int theY, int theWidth, int theHeight, const cv::String& theLabel) {
		internal::gScreen.where = theWhere;
		return internal::button(winName, internal::gScreen, theX, theY, theWidth, theHeight, theLabel, true);
	}


	bool button(cv::String winName, cvui_block_t& theBlock, int theX, int theY, int theWidth, int theHeight, const cv::String& theLabel, bool theUpdateLayout) {
		// Calculate the space that the label will fill
		cv::Size aTextSize = getTextSize(theLabel, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, nullptr);

		// Make the button bit enough to house the label
		cv::Rect aRect(theX, theY, theWidth, theHeight);

		// Render the button according to mouse interaction, e.g. OVER, DOWN, OUT.
		int aStatus = internal::iarea(winName, theX, theY, aRect.width, aRect.height);
		render::button(theBlock, aStatus, aRect, theLabel);
		render::buttonLabel(theBlock, aStatus, aRect, theLabel, aTextSize);

		// Update the layout flow according to button size
		// if we were told to update.
		if (theUpdateLayout) {
			cv::Size aSize(theWidth, theHeight);
			updateLayoutFlow(theBlock, aSize);
		}

		bool aWasShortcutPressed = false;

		//Handle keyboard shortcuts
		if (internal::gLastKeyPressed != -1) {
			// TODO: replace with something like strpos(). I think it has better performance.
			auto aLabel = internal::createLabel(theLabel);
			if (aLabel.hasShortcut && (tolower(aLabel.shortcut) == tolower((char)internal::gLastKeyPressed))) {
				aWasShortcutPressed = true;
			}
		}

		// Return true if the button was clicked
		return aStatus == cvui::CLICK || aWasShortcutPressed;
	}

	int iarea(cv::String winName, int theX, int theY, int theWidth, int theHeight) {
		cvui_mouse_t& aMouse = internal::getContext(winName).mouse;

		// By default, return that the mouse is out of the interaction area.
		int aRet = cvui::OUT;

		// Check if the mouse is over the interaction area.
		bool aMouseIsOver = cv::Rect(theX, theY, theWidth, theHeight).contains(aMouse.position);

		if (aMouseIsOver) {
			if (aMouse.anyButton.pressed) {
				aRet = cvui::DOWN;
			}
			else {
				aRet = cvui::OVER;
			}
		}

		// Tell if the button was clicked or not
		if (aMouseIsOver && aMouse.anyButton.justReleased) {
			aRet = cvui::CLICK;
		}

		return aRet;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions